How to switch position of two items in a Python list?
Posted
by mikl
on Stack Overflow
See other posts from Stack Overflow
or by mikl
Published on 2010-03-22T16:25:14Z
Indexed on
2010/03/22
16:31 UTC
Read the original article
Hit count: 345
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 if someone could come up with something a bit more elegant?
© Stack Overflow or respective owner