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:
M
Mr. Dubey     View Profile
Founder of MCQ Buddy. I just like to help others. This portal helps students in getting study material free. Share your stuff here so that others can get benefitted.