Swapping first and last items in a list
Asked Answered
I

7

6

How can I go about swapping numbers in a given list?

For example:

list = [5,6,7,10,11,12]

I would like to swap 12 with 5.

Is there an built-in Python function that can allow me to do that?

Impotence answered 29/10, 2013 at 18:51 Comment(2)
What specifies which elements you want to swap? The values (replace every 12 with a 5 and every 5 with a 12), the positions (switch the values in the first and last place), or some other rule? For example, what do you want to happen for [5,5,12,12]?Foxy
I would like the last number in the list to always be swapped with the first number in a list.... in any given list.Impotence
C
23
>>> lis = [5,6,7,10,11,12]
>>> lis[0], lis[-1] = lis[-1], lis[0]
>>> lis
[12, 6, 7, 10, 11, 5]

Order of evaluation of the above expression:

expr3, expr4 = expr1, expr2

First items on RHS are collected in a tuple, and then that tuple is unpacked and assigned to the items on the LHS.

>>> lis = [5,6,7,10,11,12]
>>> tup = lis[-1], lis[0]
>>> tup
(12, 5)
>>> lis[0], lis[-1] = tup
>>> lis
[12, 6, 7, 10, 11, 5]
Cecilececiley answered 29/10, 2013 at 18:52 Comment(3)
hello ! Thanks for the prompt reply ! Could you please explain what is happening in this piece of Code... I am fairly new at programming.Impotence
@Impotence I've added some explanation.Cecilececiley
This swapping technique also works for numpy.ndarrayDermatology
B
3

you can swap using this code,

list[0],list[-1] = list[-1],list[0]
Biosynthesis answered 30/10, 2013 at 9:52 Comment(1)
Yes. Also works the same for numpy.ndarrayDermatology
S
3

You can use "*" operator.

my_list = [1,2,3,4,5,6,7,8,9]
a, *middle, b = my_list
my_new_list = [b, *middle, a]
my_list
[1, 2, 3, 4, 5, 6, 7, 8, 9]
my_new_list
[9, 2, 3, 4, 5, 6, 7, 8, 1]

Read here for more information.

Superelevation answered 18/1, 2018 at 16:5 Comment(0)
D
0

Use the index of the number you want to change.

In [38]: t = [5,6,7,10,11,12]

In [40]: index5 = t.index(5) # grab the index of the first element that equals 5

In [41]: index12 = t.index(12) # grab the index of the first element that equals 12

In [42]: t[index5], t[index12] = 12, 5 # swap the values

In [44]: t
Out[44]: [12, 6, 7, 10, 11, 5]

Then you can make a quick swapping function

def swapNumbersInList( listOfNums, numA, numB ):
    indexA = listOfNums.index(numA)
    indexB = listOfNums.index(numB)

    listOfNums[indexA], listOfNums[indexB] = numB, numA

# calling the function
swapNumbersInList([5,6,7,10,11,12], 5, 12)
Disorient answered 29/10, 2013 at 18:55 Comment(0)
P
0

Another way (not so cute):

mylist   = [5, 6, 7, 10, 11, 12]
first_el = mylist.pop(0)   # first_el = 5, mylist = [6, 7, 10, 11, 12]
last_el  = mylist.pop(-1)  # last_el = 12, mylist = [6, 7, 10, 11]
mylist.insert(0, last_el)  # mylist = [12, 6, 7, 10, 11]
mylist.append(first_el)    # mylist = [12, 6, 7, 10, 11, 5]
Pointtopoint answered 29/10, 2013 at 19:4 Comment(0)
P
0
array = [5,2,3,6,1,12]
temp = ''
lastvalue = 5

temp = array[0]
array[0] = array[lastvalue]
array[lastvalue] = temp

print(array)

Hope this helps :)

Psychological answered 3/6, 2016 at 23:39 Comment(0)
Q
0

This is what finally worked for me.

def swap(the_list):
    temp = the_list[0]
    the_list[0] = the_list[-1]
    the_list[-1] = temp
    return the_list
Quadripartite answered 18/3, 2020 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.