How to switch position of two items in a Python list?
Asked Answered
R

8

262

I haven’t been able to find a good solution for this problem on the net (probably because switch, position, list and Python are all such overloaded words).

It’s rather simple – I have this list:

['title', 'email', 'password2', 'password1', 'first_name', 'last_name', 'next', 'newsletter']

I’d like to switch position of 'password2' and 'password1' – not knowing their exact position, only that they’re right next to one another and password2 is first.

I’ve accomplished this with some rather long-winded list-subscripting, but I wondered its possible to come up with something a bit more elegant?

Rasp answered 22/3, 2010 at 16:25 Comment(3)
Is your problem the efficiency of trying to find 'password2' in the list? Can 'password1' come before 'password2'? Is there some complexity here that doesn't come across in your original question? Otherwise I agree with @unwind.Tribunate
You should post what you've got - I'm curious about what you mean by a "rather long-winded list-subscripting."Mccully
Something along the lines of index1 = index('password1'); index2 = index('password2'); order = order[:index2].append(order[index1]).append(order[index2]).append(order[index1 + 1:]); spread on a few more lines. Pretty, no.Rasp
M
487
i = ['title', 'email', 'password2', 'password1', 'first_name', 
     'last_name', 'next', 'newsletter']
a, b = i.index('password2'), i.index('password1')
i[b], i[a] = i[a], i[b]
Mccully answered 22/3, 2010 at 16:31 Comment(0)
S
220

The simple Python swap looks like this:

foo[i], foo[j] = foo[j], foo[i]

Now all you need to do is figure what i is, and that can easily be done with index:

i = foo.index("password2")
Spence answered 22/3, 2010 at 16:30 Comment(0)
A
19

Given your specs, I'd use slice-assignment:

>>> L = ['title', 'email', 'password2', 'password1', 'first_name', 'last_name', 'next', 'newsletter']
>>> i = L.index('password2')
>>> L[i:i+2] = L[i+1:i-1:-1]
>>> L
['title', 'email', 'password1', 'password2', 'first_name', 'last_name', 'next', 'newsletter']

The right-hand side of the slice assignment is a "reversed slice" and could also be spelled:

L[i:i+2] = reversed(L[i:i+2])

if you find that more readable, as many would.

Analogous answered 22/3, 2010 at 16:35 Comment(2)
Why this is not working if you want to switch title and mail, the first element with the second?Balky
@LuigiTiburzi because when the slice-end is negative it is taken from the end of the list ie L[1:-1:-1] == L[1:7:-1] which does not work and should be L[1:-9:-1] or L[1::-1] (don't ask, I'm just starting with python and tried stuff) ~ use L[i:i+2] = L[i+1:-len(L)+(i-1):-1] instead (0 <= i <= len(L)-2).Menstruation
A
8

How can it ever be longer than

tmp = my_list[indexOfPwd2]
my_list[indexOfPwd2] = my_list[indexOfPwd2 + 1]
my_list[indexOfPwd2 + 1] = tmp

That's just a plain swap using temporary storage.

Attica answered 22/3, 2010 at 16:27 Comment(1)
If you want to be really "Pythonic" about it, you could always do this too: my_list[indexOfPwd2],my_list[indexOfPwd2+1] = my_list[indexOfPwd2+1],my_list[indexOfPwd2]Tribunate
R
0
for i in range(len(arr)):
    if l[-1] > l[i]:
        l[-1], l[i] = l[i], l[-1]
        break

as a result of this if last element is greater than element at position i then they both get swapped .

Rattle answered 7/7, 2018 at 17:49 Comment(0)
H
-1

you can use for example:

>>> test_list = ['title', 'email', 'password2', 'password1', 'first_name',
                 'last_name', 'next', 'newsletter']
>>> reorder_func = lambda x: x.insert(x.index('password2'),  x.pop(x.index('password2')+1))
>>> reorder_func(test_list)
>>> test_list
... ['title', 'email', 'password1', 'password2', 'first_name', 'last_name', 'next', 'newsletter']
Hematite answered 20/3, 2016 at 7:58 Comment(0)
N
-1

How to swap every element in a list with the next

  for i in range(len(list)):
    try:
      list[i+1]
    except IndexError:
      continue  
    else:
      list[i],list[i+1] = list[i+1],list[i]
Newberry answered 4/11, 2022 at 0:37 Comment(1)
This does not swap every element with next. It just pushes first element to the last position.Teary
U
-2

I am not an expert in python but you could try: say

i = (1,2)

res = lambda i: (i[1],i[0])
print 'res(1, 2) = {0}'.format(res(1, 2)) 

above would give o/p as:

res(1, 2) = (2,1)
Une answered 6/8, 2016 at 11:9 Comment(1)
That does not work without knowing the position of the keys to swap.Rasp

© 2022 - 2024 — McMap. All rights reserved.