Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this Python List Items Removing tutorial, we'll cover different ways to remove elements from a list. We will focus on the following methods:
remove()
methodpop()
methoddel
statementfilter()
functionremove()
methodThe remove()
method removes the first occurrence of the specified value from the list. If the value is not found, a ValueError
is raised.
my_list = [1, 2, 3, 4, 5, 3] # Remove the first occurrence of the value 3 my_list.remove(3) print(my_list) # Output: [1, 2, 4, 5, 3]
pop()
methodThe pop()
method removes and returns the item at the specified index. If the index is not provided, it removes and returns the last item in the list.
my_list = [1, 2, 3, 4, 5] # Remove and return the third item (index 2) item = my_list.pop(2) print(item) # Output: 3 print(my_list) # Output: [1, 2, 4, 5]
del
statementThe del
statement can be used to remove an item at a specific index or a slice of items from the list.
my_list = [1, 2, 3, 4, 5] # Remove the third item (index 2) del my_list[2] print(my_list) # Output: [1, 2, 4, 5] # Remove a slice of items (indices 1 to 2, inclusive) del my_list[1:3] print(my_list) # Output: [1, 5]
List comprehension can be used to create a new list by filtering out elements from an existing list based on a condition.
my_list = [1, 2, 3, 4, 5] # Create a new list without the value 3 new_list = [x for x in my_list if x != 3] print(new_list) # Output: [1, 2, 4, 5]
filter()
functionThe filter()
function filters the elements of an iterable based on a function that returns a boolean value (True or False).
my_list = [1, 2, 3, 4, 5] def not_three(x): return x != 3 filtered_list = filter(not_three, my_list) print(list(filtered_list)) # Output: [1, 2, 4, 5]
In this tutorial, you learned different ways to remove elements from a list in Python, including using the remove()
method, the pop()
method, the del
statement, list comprehension, and the filter()
function.
Deleting elements from a list in Python:
my_list = [1, 2, 3, 4, 5] del my_list[2] print(my_list) # Output: [1, 2, 4, 5]
How to remove specific elements from a list in Python:
remove
method or list comprehension.my_list = ['apple', 'orange', 'banana'] my_list.remove('orange') print(my_list) # Output: ['apple', 'banana']
Python list remove method usage:
remove
method removes the first occurrence of a specified value in a list.my_list = [6, 7, 8, 9, 10] my_list.remove(8) print(my_list) # Output: [6, 7, 9, 10]
Deleting multiple items from a list in Python:
del
or list comprehension.my_list = [11, 12, 13, 14, 15] indices_to_remove = [1, 3] my_list = [item for i, item in enumerate(my_list) if i not in indices_to_remove] print(my_list) # Output: [11, 13, 15]
Filtering and removing elements from a list in Python:
my_list = ['red', 'green', 'blue', 'red', 'yellow'] element_to_remove = 'red' filtered_list = [item for item in my_list if item != element_to_remove] print(filtered_list)
Removing duplicates from a list in Python:
my_list = [16, 17, 18, 16, 19, 20, 17] unique_list = list(set(my_list)) print(unique_list) # Output: [16, 17, 18, 19, 20]
Deleting list items by index in Python:
del
statement or list comprehension.my_list = ['cat', 'dog', 'rabbit'] index_to_remove = 1 del my_list[index_to_remove] print(my_list) # Output: ['cat', 'rabbit']
Using list comprehension to filter out elements in Python:
my_list = [21, 22, 23, 24, 25] elements_to_exclude = [22, 24] filtered_list = [item for item in my_list if item not in elements_to_exclude] print(filtered_list) # Output: [21, 23, 25]