How to switch position of two items in a Python list?
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]
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")