Python: Shift last element to first position and first element to last position in a list
Python List: Exercise - 189 with Solution
Swap First and Last Element
Write a Python program to shift last element to first position and first element to last position in a given list.
Visual Presentation:
Sample Solution:
Python Code:
# Define a function called 'shift_first_last' that shifts the first element to the last position and the last element to the first position of a list.
def shift_first_last(lst):
# Remove the first element and store it in 'x'.
x = lst.pop(0)
# Remove the last element and store it in 'y'.
y = lst.pop()
# Insert 'y' at the first position.
lst.insert(0, y)
# Insert 'x' at the last position.
lst.insert(len(lst), x)
return lst
# Create a list 'nums' containing integers.
nums = [1, 2, 3, 4, 5, 6, 7]
# Print a message indicating the original list.
print("Original list:")
# Print the original list 'nums'.
print(nums)
# Print a message indicating shifting the last element to the first position and the first element to the last position of the list.
print("Shift last element to first position and first element to last position of the said list:")
# Call the 'shift_first_last' function with 'nums' and print the result.
print(shift_first_last(nums))
# Create a list 'chars' containing characters.
chars = ['s', 'd', 'f', 'd', 's', 's', 'd', 'f']
# Print a message indicating the original list.
print("\nOriginal list:")
# Print the original list 'chars'.
print(chars)
# Print a message indicating shifting the last element to the first position and the first element to the last position of the list.
print("Shift last element to first position and first element to last position of the said list:")
# Call the 'shift_first_last' function with 'chars' and print the result.
print(shift_first_last(chars))
Sample Output:
Original list: [1, 2, 3, 4, 5, 6, 7] Shift last element to first position and first element to last position of the said list: [7, 2, 3, 4, 5, 6, 1] Original list: ['s', 'd', 'f', 'd', 's', 's', 'd', 'f'] Shift last element to first position and first element to last position of the said list: ['f', 'd', 'f', 'd', 's', 's', 'd', 's']
Flowchart:
Python Code Editor:
Previous: Write a Python program to sort a given list of tuples on specified element.
Next: Write a Python program to find the specified number of largest products from two given list, multiplying an element from each list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-189.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics