YouTip LogoYouTip

Python List Interchange

# Python3.x Python: Swap the First and Last Elements of a List [![Image 3: Document Object Reference Manual](#) Python3 Examples](#) Define a list and swap its first and last elements. For example: Before swapping: [1, 2, 3] After swapping: [3, 2, 1] ## Example 1 def swapList(newList): size =len(newList) temp = newList newList= newList newList= temp return newList newList =[1,2,3] print(swapList(newList)) The output of the above example is: [3, 2, 1] ## Example 2 def swapList(newList): newList, newList= newList, newList return newList newList =[1,2,3] print(swapList(newList)) The output of the above example is: [3, 2, 1] ## Example 3 def swapList(list): get =list,list list,list= get return list newList =[1,2,3] print(swapList(newList)) The output of the above example is: [3, 2, 1] [![Image 4: Document Object Reference Manual](#) Python3 Examples](#)
← Cpp Return Arrays From FunctioPython3 Sum Array β†’