Move an item inside a list?
A slightly shorter solution, that only moves the item to the end, not anywhere is this:
l += [l.pop(0)]
For example:
>>> l = [1,2,3,4,5]
>>> l += [l.pop(0)]
>>> l
[2, 3, 4, 5, 1]
Use the insert
method of a list:
l = list(...)
l.insert(index, item)
Alternatively, you can use a slice notation:
l[index:index] = [item]
If you want to move an item that's already in the list to the specified position, you would have to delete it and insert it at the new position:
l.insert(newindex, l.pop(oldindex))