Python program to swap the first and the last element of a list
Python program to swap the first and the last element of a list :
Python list is one of the most frequently used datatypes. A list is used to hold a group of elements. These elements are placed inside a square bracket separated by commas. Python list can hold items of different datatypes e.g. a list can have items of type ‘integer’, ‘float’, ‘string’ etc.
Unlike strings, we can change the items of a list. The square bracket is used to access an element of a list. We need to pass the index of the element for that. For example, ‘my_list[i]’ will return the item on index ‘i’ for the list ‘my_list’. The index starts from ‘0’, i.e. ‘0’ is the index of the first element, ‘1’ is the index of the second element etc.
In this python programming tutorial, we will learn how to swap the first and the last element of a list. The list is already given to the program. It will swap the elements and print out the new list.
Python program :
If you know how to swap two numbers in python or any other language, this program uses the same logic. Create one temporary variable to hold the last value, assign the first element value to the last element and assign the temporary element value to the first element. That’s it.
Let’s take a look at the program :
#1 user_list = [1,2,5,4,6,7,8] #2 temp = user_list[len(user_list) - 1] #3 user_list[len(user_list) - 1] = user_list #4 user_list = temp #5 print(user_list)
(You can also download the source code from here) :
The commented numbers in the above program denote the step numbers below :
- user_list is the given list holding 7 numbers. Our program will swap the first and the last element of this list. This is a predefined list and we are using only numbers in this list. You can also take the list elements as input from the user. This list can also hold different datatypes.
- Get the last element of the list. This is the element of index position (length of the list – 1). The index starts from ‘0’. The index of the 1st element is ‘0’, the 2nd element is ‘1’ etc. So, the index of the last element is (length – 1). For calculating the list length, we are using len() method. This method takes the list as its argument and returns its length. Assign the value of the last element to a temporary variable ‘temp’. In this step, ‘temp’ is holding the last element’s value.
- Assign the value of the first element of the list to the last element. We are assigning the index ‘0’ element or the first element to the index ‘len(user_list) – 1’ element or the last element. ‘=’ operator is used for the assignment.
- Now, set the value stored in the temporary variable to the first element of the list.
- Finally, print out the list.
It will print the below output :
[8, 2, 5, 4, 6, 7, 1]
In this tutorial, we have learned how to swap the first and the last element of a list. Similar to this program, we can swap any two elements of a list in python easily. Go through the program and drop one comment below if you have any queries.
Similar tutorials :
- Python swap case of each character of a string
- How to read a user input list in python
- Python program to insert multiple elements to a list at any specific position
- Python program to swap the first and the last character of a string
- How to find the intersection of two list elements in python
- Python flatten a nested list or list of lists