Zabi Rahman
Zabi Rahman

Reputation: 1

Trying to swap the first and last elements of a list in Python, but it works only for a predefined list. Not for a list of randomly generated numbers

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.

Upvotes: -1

Views: 640

Answers (4)

Riccardo Bucco
Riccardo Bucco

Reputation: 15384

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.

If you want to retain the original list l, and also return a separate swapped list l1, you have to create a new list! Here is how you can do it:

import random

def generateList(size):
    return [random.randint(0, 99) for _ in range(size)]

def swapFirstAndLast(list_to_be_swapped):
    new_list = list_to_be_swapped.copy()
    new_list[0], new_list[-1] = new_list[-1], new_list[0]
    return new_list

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

Output:

[38, 59, 86, 26, 19]
[38, 59, 86, 26, 19] [19, 59, 86, 26, 38]

Upvotes: 1

lroth
lroth

Reputation: 367

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]

Upvotes: -1

Darren Tu
Darren Tu

Reputation: 1

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!

Upvotes: 0

grymlin
grymlin

Reputation: 492

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)

Upvotes: 0

Related Questions