How to remove an item from a list in Python
This tutorial is on How to remove an item from a list in Python. We use list methods clear(), pop(), and remove() to remove items from a list. and use also possible to delete items using a del statement by range or specifying a position with an index or slice.
clear() to remove all items.
pop() to remove an item by index and return get its value.
remove() to remove an item by value.
del to remove items by index or slice.
List comprehensions to remove items’ condition.
clear()
You can remove all items from the list using the clear() method.
lst = list(range(5)) print(lst) # Output: [0, 1, 2, 3, 4] lst.clear() print(lst) # Output: []
pop()
You can remove the item at the range or specify a position and get the return value with the pop() method.
Note: The first index is 0 (zero-based indexing).
lst = list(range(5)) print(lst) # Output: [0, 1, 2, 3, 4] print(lst.pop(0))# Output:
0 print(lst)# Output:
[1, 2, 3, 4] print(lst.pop(3))# Output:
4 print(lst)# Output:
[1, 2, 3]
And You can use negative values to specify the position from the end element.
lst = list(range(5)) print(lst.pop(-3))# Output:
2 print(lst) #Output:
[0, 1, 3, 4]
If you can not add an argument it will last item delete.
lst = list(range(5)) print(lst.pop()) # Output: 4 print(lst) # Output: [0, 1, 2, 3]
You can pass a nonexistent value, so it will get raises an error.
lst = list(range(5)) lst.pop(16) # IndexError: pop index out of range
remove()
remove() method to you can remove the first item from the list. and where its value is equal to the specified value are same it will remove.
lst = ['One', 'Two', 'Three', 'Four', 'Five'] print(lst) # Output: ['One', 'Two', 'Three', 'Four', 'Five'] lst.remove('Three') print(lst) # Output: ['One', 'Two', 'Four', 'Five']
If the list in more than one matching value, it will only be the first one deleted.
lst = ['One', 'Two', 'Three', 'Four', 'Five', 'Two'] print(lst) # Output: ['One', 'Two', 'Three', 'Four', 'Five', 'Two'] lst.remove('Two') print(lst) # Output: ['One', 'Three', 'Four', 'Five', 'Two']
You can pass a nonexistent value, so it will get raises an error.
lst = ['One', 'Two', 'Three', 'Four', 'Five'] lst.remove('Devnote') # ValueError: list.remove(x): x not in list
del
del statement is used to specify the item to be deleted by index. clear(), pop(), and remove() are methods of list. And You can also remove elements from a list using del statements.
Note: The first index is 0, and the last index is -1.
lst = list(range(5)) print(lst) # Output: [0, 1, 2, 3, 4] del lst[1] print(lst) # Output: [0, 2, 3, 4] del lst[-1] print(lst) # Output: [0, 2, 3] del lst[2] print(lst) # Output: [0, 2]
And you can delete multiple items using slice.
lst = list(range(5)) print(lst) # Output: [0, 1, 2, 3, 4] # Get the list from position 1 to position 3 del lst[1:3] print(lst) # Output: [0, 3, 4] lst = list(range(5)) # Get the list from position 2 to start position del lst[:2] # Output: [2, 3, 4] lst = list(range(5)) # Get the list from position 2 to end position del lst[2:] # Output: [0, 1]
And you can also possibly delete all items using specifying the entire range.
lst = list(range(5)) # Entire range del lst[:] # Output: []
del statement also specifies step as [start:stop:step].
lst = list(range(7)) # Get the list from position 2 and position 6 stop and 2 step del lst[2:6:2] print(lst) # Output: [0, 1, 3, 5, 6] lst = list(range(7)) del lst[::2] print(lst) # Output: [1, 3, 5]
List comprehensions
List comprehension is used of removing items that satisfy the condition is equivalent to extracting items. here removing odd or even items. % Is the remainder operator and I % 2 is the remainder of dividing I by 2.
lst = list(range(7)) print(lst) # Output: [0, 1, 2, 3, 4, 5, 6] # odd value print([i for i in lst if i % 2 != 0]) # Output: [1, 3, 5] # even value print([i for i in lst if i % 2 == 0]) # Output: [0, 2, 4, 6] print(lst) # Output: [0, 1, 2, 3, 4, 5, 6]
Other examples
lst = ['One', 'Two', 'Three', 'Four', 'Five', 'Two'] print(lst) # Output: ['One', 'Two', 'Three', 'Four', 'Five', 'Two'] print([chr for chr in lst if chr != 'Three']) # Output: ['One', 'Two', 'Four', 'Five', 'Two'] print([chr for chr in lst if chr.endswith('e')]) # Output: ['One', 'Three', 'Five']
If you want to remove duplicate elements, use the set() function.
lst = ['One', 'Two', 'Three', 'Four', 'Five', 'Two'] print(list(set(lst))) # Output: ['Four', 'Five', 'Three', 'Two', 'One']