Fix Python – Move an item inside a list?


Asked By – Gabriele Cirulli

In Python, how do I move an item to a definite index in a list?

Now we will see solution for issue: Move an item inside a list?


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))

This question is answered By – David Z

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0