How to Move an Element in a List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
8 min

banner

# Table of Contents

  1. Move an item in a List in Python
  2. Move an item to the front of the List in Python
  3. Move an element to the end of a List in Python
  4. Move items from one List to another in Python

# Move an item in a List in Python

To move an item in a list:

  1. Use the list.remove() method to remove the item from the list.
  2. Use the list.insert() method to insert the value into the list at a specific position.
main.py
my_list = ['bobby', 'hadz', '.', 'com'] # โœ… Move an item in a list item = 'hadz' my_list.remove(item) my_list.insert(0, item) print(my_list) # ๐Ÿ‘‰๏ธ ['hadz', 'bobby', '.', 'com'] # -------------------------------------------- # โœ… Insert a value into a list at a specific position my_list = ['bobby', '.', 'com'] my_list.insert(1, 'hadz') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

move item in list

The code for this article is available on GitHub

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

main.py
my_list = ['bobby', 'hadz', '.', 'com'] my_list.remove('hadz') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', '.', 'com']

The method raises a ValueError if there is no such item.

The remove() method mutates the original list and returns None.

The last step is to use the list.insert() method to insert the value into the list at a specific position.

main.py
my_list = ['bobby', 'hadz', '.', 'com'] item = 'hadz' my_list.remove(item) my_list.insert(0, item) print(my_list) # ๐Ÿ‘‰๏ธ ['hadz', 'bobby', '.', 'com']

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
Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

# Inserting a value into a list at a specific index

If you simply need to insert a value into a list at a specific position, use the list.insert() method directly.

main.py
my_list = ['bobby', '.', 'com'] my_list.insert(1, 'hadz') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']

inserting value into list at specific index

The code for this article is available on GitHub

# Removing an item from a list by index

If you need to remove an item from a list by index rather than by value, use the list.pop() method.

main.py
my_list = ['bobby', 'hadz', '.', 'com'] my_list.insert( 0, my_list.pop(1) ) print(my_list) # ๐Ÿ‘‰๏ธ ['hadz', 'bobby', '.', 'com']

removing item from list by index

The code for this article is available on GitHub

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

main.py
my_list = ['bobby', 'hadz', '.', 'com'] print(my_list.pop(1)) # ๐Ÿ‘‰๏ธ hadz print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', '.', 'com']

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

main.py
my_list = ['bobby', 'hadz', '.', 'com'] print(my_list.pop()) # ๐Ÿ‘‰๏ธ com print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.']

# Move an item to the front of the List in Python

If you need to move an item to the front of the list:

  1. Use the list.remove() method to remove the item from the list.
  2. Use the list.insert() method to insert the value at index 0.
main.py
my_list = ['bobby', 'hadz', '.', 'com'] item = 'hadz' my_list.remove(item) my_list.insert(0, item) print(my_list) # ๐Ÿ‘‰๏ธ ['hadz', 'bobby', '.', 'com']

move item to front of list

The code for this article is available on GitHub

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

main.py
my_list = ['bobby', 'hadz', '.', 'com'] my_list.remove('hadz') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', '.', 'com']

The method raises a ValueError if there is no such item.

The remove() method mutates the original list and returns None.

# Handling a scenario where the item is not in the list

You can use a try/except statement if you need to handle a scenario where the item is not in the list.

main.py
my_list = ['bobby', 'hadz', '.', 'com'] try: item = 'another' my_list.remove(item) my_list.insert(0, item) except ValueError: # ๐Ÿ‘‡๏ธ this runs print('The specified value is not in the list') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']
The code for this article is available on GitHub

The specified value is not present in the list, so a ValueError exception is raised and is then handled by the except block.

The last step is to use the list.insert() method to insert the value at index 0.

main.py
my_list = ['bobby', 'hadz', '.', 'com'] item = 'hadz' my_list.remove(item) my_list.insert(0, item) print(my_list) # ๐Ÿ‘‰๏ธ ['hadz', 'bobby', '.', 'com']

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
Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

The list.remove() method removes a list item by value.

If you need to remove a list item by an index, use the list.pop() method.

# Move an item to the front of the List using list.pop()

This is a three-step process:

  1. Use the list.index() method to get the index of the item.
  2. Use the list.pop() method to remove and retrieve the item from the list.
  3. Use the list.insert() method to insert the value at index 0.
main.py
my_list = ['bobby', 'hadz', '.', 'com'] item = 'hadz' my_list.insert( 0, my_list.pop(my_list.index(item)) ) print(my_list) # ๐Ÿ‘‰๏ธ ['hadz', 'bobby', '.', 'com']
The code for this article is available on GitHub

The list.index() method returns the index of the first item whose value is equal to the provided argument.

main.py
my_list = ['bobby', 'hadz', '.', 'com'] print(my_list.index('hadz')) # ๐Ÿ‘‰๏ธ 1

The method raises a ValueError if there is no such item in the list.

We used the list.pop() method to remove the item from the list.

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

main.py
my_list = ['bobby', 'hadz', '.', 'com'] print(my_list.pop(1)) # ๐Ÿ‘‰๏ธ 'hadz' print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', '.', 'com']

The last step is to use the list.insert() method to insert the removed item at index 0.

If you need to move the last item in a list to the front, call the list.pop() method without any arguments.

main.py
my_list = ['bobby', 'hadz', '.', 'com'] my_list.insert(0, my_list.pop()) print(my_list) # ๐Ÿ‘‰๏ธ ['com', 'bobby', 'hadz', '.']

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

# Handling a scenario where the list is empty

Note that the list.pop() method raises an IndexError exception if called on an empty list.

You can use an if statement to make sure the list isn't empty before calling pop().

main.py
my_list = [] if my_list: my_list.insert(0, my_list.pop()) print(my_list) # ๐Ÿ‘‰๏ธ []
The code for this article is available on GitHub

The if statement makes sure the list isn't empty before calling the list.pop() method.

# Move an element to the end of a List in Python

If you need to move an element to the end of a list:

  1. Use the list.append() method to append the value to the end of the list.
  2. Use the del statement to delete the original element from the list.
main.py
my_list = ['bobby', 'hadz', '.', 'com'] element = 'hadz' my_list.append(element) del my_list[my_list.index(element)] print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', '.', 'com', 'hadz']
The code for this article is available on GitHub

We used the list.append() method to append the value to the end of the list.

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

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

The method returns None as it mutates the original list.

The next step is to get the index of the original element and remove the element from the list.

main.py
my_list = ['bobby', 'hadz', '.', 'com'] element = 'hadz' print(my_list.index(element)) # ๐Ÿ‘‰๏ธ 1
The list.index() method returns the index of the first item whose value is equal to the provided argument.

The last step is to use the del statement to delete the list item at the index.

main.py
my_list = ['bobby', 'hadz', '.', 'com'] element = 'hadz' my_list.append(element) del my_list[my_list.index(element)] print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', '.', 'com', 'hadz']

The del statement removes an item from a list by index.

main.py
my_list = ['bobby', 'hadz', '.', 'com'] del my_list[0] print(my_list) # ๐Ÿ‘‰๏ธ ['.', 'com', 'hadz']

Alternatively, you can use the list.remove() method.

# Move an element to the end of a List using list.remove()

This is a two-step process:

  1. Use the list.append() method to append the value to the end of the list.
  2. Use the list.remove() method to remove the original item from the list.
main.py
my_list = ['bobby', 'hadz', '.', 'com'] element = 'hadz' my_list.append(element) my_list.remove(element) print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', '.', 'com', 'hadz']
The code for this article is available on GitHub

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

main.py
my_list = ['bobby', 'hadz', '.', 'com'] my_list.remove('hadz') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', '.', 'com']

The remove() method mutates the original list and returns None.

# Move items from one List to another in Python

If you need 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']
The code for this article is available on GitHub
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 using a for loop

This is a four-step process:

  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 code for this article is available on GitHub

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've also written an article on how to shift (rotate) a list in Python.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev