list swap python code example
Example 1: swap two elements in a list python
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]
Example 2: python swap function
def swap0(s1, s2):
assert type(s1) == list and type(s2) == list
tmp = s1[:]
s1[:] = s2
s2[:] = tmp
# However, the easier and better way to do a swap in Python is simply:
s1, s2 = s2, s1
Example 3: how to swap in list in python
list[pos1], list[pos2] = list[pos2], list[pos1]