Last updated: Apr 10, 2024
Reading timeยท8 min
To move an item in a list:
list.remove()
method to remove the item from the list.list.insert()
method to insert the value into the list at a
specific position.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']
The list.remove() method removes the first item from the list whose value is equal to the passed in argument.
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.
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:
Name | Description |
---|---|
index | The index of the element before which to insert |
item | The item to be inserted at the given index |
0
, and the last item has an index of -1
or len(my_list) - 1
.If you simply need to insert a value into a list at a specific position, use the
list.insert()
method directly.
my_list = ['bobby', '.', 'com'] my_list.insert(1, 'hadz') print(my_list) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
If you need to remove an item from a list by index rather than by value, use the
list.pop()
method.
my_list = ['bobby', 'hadz', '.', 'com'] my_list.insert( 0, my_list.pop(1) ) print(my_list) # ๐๏ธ ['hadz', 'bobby', '.', 'com']
The list.pop()
method removes the item at the given position in the list and
returns it.
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.
my_list = ['bobby', 'hadz', '.', 'com'] print(my_list.pop()) # ๐๏ธ com print(my_list) # ๐๏ธ ['bobby', 'hadz', '.']
If you need to move an item to the front of the list:
list.remove()
method to remove the item from the list.list.insert()
method to insert the value at index 0
.my_list = ['bobby', 'hadz', '.', 'com'] item = 'hadz' my_list.remove(item) my_list.insert(0, item) print(my_list) # ๐๏ธ ['hadz', 'bobby', '.', 'com']
The list.remove() method removes the first item from the list whose value is equal to the passed in argument.
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
.
You can use a try/except statement if you need to handle a scenario where the item is not in the list.
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 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
.
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:
Name | Description |
---|---|
index | The index of the element before which to insert |
item | The item to be inserted at the given index |
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.
This is a three-step process:
list.index()
method to get the index of the item.list.pop()
method to remove and retrieve the item from the list.list.insert()
method to insert the value at index 0
.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 list.index()
method returns the index of the first item whose value is
equal to the provided argument.
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.
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.
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.
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()
.
my_list = [] if my_list: my_list.insert(0, my_list.pop()) print(my_list) # ๐๏ธ []
The if
statement makes sure the list isn't empty before calling the
list.pop()
method.
If you need to move an element to the end of a list:
list.append()
method to append the value to the end of the list.del
statement to delete the original element from the list.my_list = ['bobby', 'hadz', '.', 'com'] element = 'hadz' my_list.append(element) del my_list[my_list.index(element)] print(my_list) # ๐๏ธ ['bobby', '.', 'com', 'hadz']
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.
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.
my_list = ['bobby', 'hadz', '.', 'com'] element = 'hadz' print(my_list.index(element)) # ๐๏ธ 1
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.
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.
my_list = ['bobby', 'hadz', '.', 'com'] del my_list[0] print(my_list) # ๐๏ธ ['.', 'com', 'hadz']
Alternatively, you can use the list.remove()
method.
This is a two-step process:
list.append()
method to append the value to the end of the list.list.remove()
method to remove the original item from the list.my_list = ['bobby', 'hadz', '.', 'com'] element = 'hadz' my_list.append(element) my_list.remove(element) print(my_list) # ๐๏ธ ['bobby', '.', 'com', 'hadz']
The list.remove() method removes the first item from the list whose value is equal to the passed in argument.
my_list = ['bobby', 'hadz', '.', 'com'] my_list.remove('hadz') print(my_list) # ๐๏ธ ['bobby', '.', 'com']
The remove()
method mutates the original list and returns None
.
If you need to move items from one list to another:
list.remove()
method to remove an item from the first list.list.append()
method to append the value to the second list.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 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.
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.
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:
Name | Description |
---|---|
index | The index of the element before which to insert |
item | The 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()
.
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.
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.
This is a four-step process:
enumerate()
function to get access to the index of the current
iteration.list.remove()
method to remove items from the list.list.append()
method to append items to the second list.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.
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.
You can learn more about the related topics by checking out the following tutorials: