Move an item inside a list?


Python Problem Overview

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

Python Solutions

Solution 1 - Python

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

Solution 2 - Python

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]

Solution 3 - Python

If you don't know the position of the item, you may need to find the index first:

old_index = list1.index(item)

then move it:

list1.insert(new_index, list1.pop(old_index))

or IMHO a cleaner way:

list1.insert(new_index, item)

Solution 4 - Python

A solution very simple, but you have to know the index of the original position and the index of the new position:


Solution 5 - Python

I profiled a few methods to move an item within the same list with timeit. Here are the ones to use if j>i:

│ 14.4usec │ x[i:i]=x.pop(j),     │
│ 14.5usec │ x[i:i]=[x.pop(j)]    │
│ 15.2usec │ x.insert(i,x.pop(j)) │

and here the ones to use if j<=i:

│ 14.4usec │ x[i:i]=x[j],;del x[j]     │
│ 14.4usec │ x[i:i]=[x[j]];del x[j]    │
│ 15.4usec │ x.insert(i,x[j]);del x[j] │

Not a huge difference if you only use it a few times, but if you do heavy stuff like manual sorting, it's important to take the fastest one. Otherwise, I'd recommend just taking the one that you think is most readable.

Solution 6 - Python

l = list(...) #replace ... with the list contents
if item in l: #Checks if the item to be moved is present in the list
    l.remove(item) #  Removes the item from the current list if the previous line's conditions are achieved
l.insert(new_index,item) # Adds item to new list

Solution 7 - Python

I'd prefer to do it in one expression like this:

>>> l = [1,2,3,4,5]
>>> [*l, l.pop(0)]
[2, 3, 4, 5, 1]

Solution 8 - Python

A forwards(left to right) swapping example:

>>> abc = [1,2,3,4,5]
>>> abc.insert(0, abc.pop(len(abc)-1))

[5, 1, 2, 3, 4]


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGabriele CirulliView Question on Stackoverflow
Solution 1 - PythonDavid ZView Answer on Stackoverflow
Solution 2 - PythonTimView Answer on Stackoverflow
Solution 3 - PythonnngeekView Answer on Stackoverflow
Solution 4 - PythonV.PetrettoView Answer on Stackoverflow
Solution 5 - PythonRiedlerView Answer on Stackoverflow
Solution 6 - PythonPranav KumarView Answer on Stackoverflow
Solution 7 - PythonU12-ForwardView Answer on Stackoverflow
Solution 8 - PythonTunahan BilgiçView Answer on Stackoverflow