Welcome folks today in this example we will be
interchanging first and last elements inside a list in python. All the full source code of the application is given below.
In order to get started you need to create an
app.py file and copy paste the following code
# Python3 program to swap first # and last element of a list # Swap function def swapList(newList): size = len(newList) # Swapping temp = newList newList = newList[size - 1] newList[size - 1] = temp return newList # Driver code newList = [12, 35, 9, 56, 24] print("The original list is \n") print(newList) print("\n") print("The swapped list is ") print(swapList(newList))
Now if you execute the above python script by typing the below command as shown below
Now you can see that we have successfully
swapped the first and last element inside the list