Home > other >  Trying to swap the first and last elements of a list in Python, but it works only for a predefined l
Trying to swap the first and last elements of a list in Python, but it works only for a predefined l

Time:08-31

I was trying to create a python program which swaps the first and last elements of a list. I passed a pre-created list into the algorithm and it worked perfectly. Here's my code:

        def swapFirstAndLast(list_to_be_swapped):
            size = len(list_to_be_swapped)
            list_to_be_swapped[0],list_to_be_swapped[size-1] = list_to_be_swapped[size-1],list_to_be_swapped[0]
            return list_to_be_swapped

l = [12,33,42,76,46,97]
swapFirstAndLast(l)
print(l)

Output: [97, 33, 42, 76, 46, 12]

Then I tried to create functions; one function to create a list of randomly generated numbers, and the second function to perform the swapping operation. Although everything makes sense to me, it is not performing the swapping operation now. This is the code I came up with:

import random

def generateList(size):
    list1 = []
    for i in range(size):
        list1.append(random.randint(0,99))
    return list1

def swapFirstAndLast(list_to_be_swapped):
    size = len(list_to_be_swapped)
    list_to_be_swapped[0],list_to_be_swapped[size-1] = list_to_be_swapped[size-1],list_to_be_swapped[0]
    return list_to_be_swapped

l = generateList(5)
l1 = swapFirstAndLast(l)
print(l,l1)

Output: [49, 78, 63, 82, 72] [49, 78, 63, 82, 72]

As you can see, it does not perform the swapping operation now. I am not able to understand where I am going wrong.

CodePudding user response:

You are swapping the first and the last element of the initial list (i.e., l) too! Please look at this slightly modified example:

import random

def generateList(size):
    list1 = []
    for i in range(size):
        list1.append(random.randint(0,99))
    return list1

def swapFirstAndLast(list_to_be_swapped):
    size = len(list_to_be_swapped)
    list_to_be_swapped[0],list_to_be_swapped[size-1] = list_to_be_swapped[size-1],list_to_be_swapped[0]
    return list_to_be_swapped

l = generateList(5)
print(l)
l1 = swapFirstAndLast(l)
print(l, l1)

Output:

[54, 14, 3, 38, 87]
[87, 14, 3, 38, 54] [87, 14, 3, 38, 54]

As you can see, the list l has been changed.

The thing here is that you are not creating a new list, but you're modifying the existing one. It doesn't matter if it has a different name within the function.

CodePudding user response:

your program works ! your function just modifies the list directly, you can see it better if you do this :

l = generateList(5)
print(l)
l1 = swapFirstAndLast(l)
print(l1)

CodePudding user response:

It turns out that you have already swapped the list (i.e. l) it's just when your print (l,l1) that it looks like you haven't swapped it because it's printing the swapped version of (l). put the print(l) line above ( l1 = swapFirstAndLast(l) ) to see it!

CodePudding user response:

the swapping can be done by using index:

def swapFirstAndLast(lst):
    lst[0], lst[-1] = lst[-1], lst[0]
    return lst

lst = [12,33,42,76,46,97]
print(swapFirstAndLast(lst))

result is: [97, 33, 42, 76, 46, 12]

  • Related