You are here: Home / Topics / Python program to swap first and last element of a list

Python program to swap first and last element of a list

Filed under: Python on 2023-09-17 22:25:39

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Python3 program to swap first
# and last element of a list

# Swap function


def swapList(newList):
   size = len(newList)

   # Swapping

   temp = newList[0]
   newList[0] = newList[size - 1]
   newList[size - 1] = temp

   return newList


# Driver code

newList = [12, 35, 9, 56, 24]

print swapList(newList)

About Author:
S
Shyam Dubey     View Profile
If you are good in any field. Just share your knowledge with others.