Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move an item inside a list?

Tags:

python

list

People also ask

How do you move items in a list in Python?

In Python, the easiest way to shift values in a list is with the Python list pop(), insert(), and append() functions. You can also use the deque() data structure from the Python collections module to shift a list. You can also use list slicing to shift a list forward or backwards in Python.

How do I move an element to a list?

Method : Using pop() + insert() + index() This particular task can be performed using combination of above functions. In this we just use the property of pop function to return and remove element and insert it to the specific position of other list using index function.

How do you move an item in a list to the end of the list in Python?

The append function adds the element removed by pop function using the index provided by index function. The sort method can also be used to achieve this particular task in which we provide the key as equal to the string we wish to shift so that it is moved to the end.

How do you move an item to the front of a list in Python?

insert() To prepend to a list in Python, use the list. insert() method with index set to 0, which adds an element at the beginning of the list. The insert() is a built-in Python function that inserts an item to the list at the given index.


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

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]

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.remove(item)
list1.insert(new_index, item)

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

list1[index1],list1[index2]=list1[index2],list1[index1]

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.