Skip to main content

How to Move Items in Lists in Python

This guide explores various techniques for moving items within a list and between lists in Python. We'll cover methods for moving items to specific positions, handling edge cases, and transferring items between lists, using the core list manipulation methods such as remove(), insert(), pop(), and append().

Moving an Item Within a List

To move an item within a list, you can remove it and re-insert it at the desired location.

my_list = ['tutorial', 'reference', '.', 'com']
item = 'reference'
my_list.remove(item)
my_list.insert(0, item)
print(my_list) # Output: ['reference', 'tutorial', '.', 'com']
  • The remove() method deletes the first matching item, and the insert() method inserts the item at the specified index, moving all other elements to the right.

1. Inserting an Item at a Specific Index

If you only need to insert a value into a list at a specific index, use the insert() method directly:

my_list = ['tutorial', '.', 'com']
my_list.insert(1, 'reference')
print(my_list) # Output: ['tutorial', 'reference', '.', 'com']
  • The insert() method inserts reference before the element at index 1.

2. Removing an item by index

You can use the pop() method to remove an item at an index:

my_list = ['tutorial', 'reference', '.', 'com']

item = 'reference'
my_list.insert(0, my_list.pop(1))
print my_list # Output: ['reference', 'tutorial', '.', 'com']

  • my_list.pop(1) removes the item at index 1 and returns it.
  • Then the returned item from pop() is inserted at the index 0 using the insert method.

Moving an Item to the Front of a List

To move an item to the front of the list, remove the item and re-insert it at index 0.

my_list = ['tutorial', 'reference', '.', 'com']
item = 'reference'
my_list.remove(item)
my_list.insert(0, item)
print(my_list) # Output: ['reference', 'tutorial', '.', 'com']

1. Handling Missing Items

If the item might not be in the list, use a try/except block:

my_list = ['tutorial', 'reference', '.', 'com']
try:
item = 'another'
my_list.remove(item)
my_list.insert(0, item)
except ValueError:
print('The specified value is not in the list')
print(my_list)

Output:

The specified value is not in the list
['tutorial', 'reference', '.', 'com']

2. Using pop() and insert()

An alternative approach is to use the pop() and insert() methods instead:

my_list = ['tutorial', 'reference', '.', 'com']
item = 'reference'
my_list.insert(
0,
my_list.pop(my_list.index(item))
)
print(my_list) # Output: ['reference', 'tutorial', '.', 'com']
  • The index() method gets the index of the specified value.
  • The pop() method removes an item at a given index and returns it.

3. Using pop() to Move Last Item to Front

If you want to move the last element to the front, call the pop() method without arguments:

my_list = ['tutorial', 'reference', '.', 'com']
my_list.insert(0, my_list.pop())
print(my_list) # Output: ['com', 'tutorial', 'reference', '.']
  • The method pop() method removes and returns the last element.

4. Handling Empty Lists

If your list might be empty, use the if statement to prevent exceptions:

my_list = []
if my_list:
my_list.insert(0, my_list.pop())
print(my_list) # Output: []

Moving an Item to the End of a List

To move an item to the end of a list, append it and remove it from the old position.

1. Using append() and del

my_list = ['tutorial', 'reference', '.', 'com']
element = 'reference'
my_list.append(element)
del my_list[my_list.index(element)]
print(my_list) # Output: ['tutorial', '.', 'com', 'reference']
  • The append() method adds an item to the end.
  • The del statement removes an element from a given index.

2. Using append() and remove()

You can also use the remove() method to remove the item from the old position:

my_list = ['tutorial', 'reference', '.', 'com']
element = 'reference'
my_list.append(element)
my_list.remove(element)
print(my_list) # Output: ['tutorial', '.', 'com', 'reference']
  • The remove() method removes the first matching item in the list.

Moving Items Between Lists

To transfer items between two lists, use a combination of remove() or pop() and append() or insert().

1. Using pop() and append()

list_1 = ['com']
list_2 = ['tutorial', 'reference']
list_2.append(list_1.pop(0))
print(list_1) # Output: []
print(list_2) # Output: ['tutorial', 'reference', 'com']
  • The pop() method removes and returns the item from index 0 in list_1, which is then appended to list_2.

2. Moving Multiple Items with a for Loop

To move multiple items based on a specific condition, use a for loop with enumerate():

list_1 = ['a', 'b', 'c', 'd']
list_2 = [1, 2, 3, 4, 5]
items_to_move = ['b', 'c', 'd']

for index, item in list(enumerate(list_1)):
if item in items_to_move:
list_1.remove(item)
list_2.append(item)

print(list_1) # Output: ['a']
print(list_2) # Output: [1, 2, 3, 4, 5, 'b', 'c', 'd']
  • The enumerate() provides both the index and the element for iteration, which helps to remove specific values.
  • You have to cast the result of enumerate() to a list, or you won't be able to change its size.
  • list_1.remove(item) removes item from list 1.
  • The list_2.append(item) adds the item to the end of list_2.