Move items from one list to another in Python

avatar

Borislav Hadzhiev

Last updated: Sep 22, 2022

banner

Photo from Unsplash

Move items from one list to another in Python #

To move items from one list to another:

  1. Use the list.remove() method to remove an item from the first list.
  2. Use the list.append() method to append the value to the second list.
main.py
list_1 = ['com'] list_2 = ['bobby', 'hadz'] list_1.remove('com') list_2.append('com') print(list_1) # 👉️ [] print(list_2) # 👉️ ['bobby', 'hadz', 'com']
If you need to move multiple items from one list to another, scroll down to the next subheading.

The example uses the list.remove() method to remove an item by value.

The list.remove() method removes the first item from the list whose value is equal to the passed in argument.

main.py
list_1 = ['com'] list_2 = ['bobby', 'hadz'] list_1.remove('com') list_2.append('com') print(list_1) # 👉️ [] print(list_2) # 👉️ ['bobby', 'hadz', 'com']

The list.append() method adds an item to the end of the list.

If you need to insert the item at a specific position, use the list.insert() method.

main.plist_1
list_2 = ['bobby', 'hadz'] list_1.remove('com') list_2.insert(0, 'com') print(list_1) # 👉️ [] print(list_2) # 👉️ ['com', 'bobby', 'hadz']y

The list.insert method inserts an item at a given position.

The method takes the following 2 parameters:

NameDescription
indexThe index of the element before which to insert
itemThe item to be inserted at the given index

If you need to remove the item from the first list by index, use the list.pop() method instead of list.remove().

main.py
list_1 = ['com'] list_2 = ['bobby', 'hadz'] list_2.append(list_1.pop(0)) print(list_1) # 👉️ [] print(list_2) # 👉️ ['bobby', 'hadz', 'com']

The list.pop method removes the item at the given position in the list and returns it.

main.py
my_list = ['bobby', 'hadz', 'com'] my_list.pop() print(my_list) # 👉️ ['bobby', 'hadz']

If no index is specified, the pop() method removes and returns the last item in the list.

Move multiple items from one list to another in Python #

To move multiple items from one list to another:

  1. Use the enumerate() function to get access to the index of the current iteration.
  2. Iterate over the first list.
  3. Use the list.remove() method to remove items from the list.
  4. Use the list.append() method to append items to the second list.
main.py
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) # 👉️ ['a'] print(list_2) # 👉️ [1, 2, 3, 4, 5, 'b', 'c', 'd']

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # 👉️ 0 bobby, 1 hadz, 2 com

We used the list() class because we can't directly iterate over the enumerate object and remove items from the list.

Changing a list's length while iterating over it is not allowed.

On each iteration, we use the list.remove() method to remove an item from the first list and use the list.append() method to append the item to the second list.

After the last iteration, all of the specified items are moved from the first to the second list.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.