Interchange First Last Element

 def swap (ls1):

    if len(ls1) >= 2:
        ls1[0],ls1[-1]=ls1[-1],ls1[0]
        return ls1

my_list=[1,2,3,4,5]
print("Before swap :",my_list)
swap_list=swap(my_list)
print("After swap :",swap_list)



    Output:-

    Before swap : [1, 2, 3, 4, 5]
    After swap : [5, 2, 3, 4, 1]





Post a Comment

0 Comments