Remove multiple elements from a list in Python

Removing multiple elements from a Python list: In this tutorial, we will learn how to remove multiple elements from a list based on the given indices, values, patterns, or calculations. Learn with the help of different approaches and examples. By IncludeHelp Last updated : June 28, 2023

Remove multiple elements from a list

Removing multiple elements from a Python list is useful when you want to remove unwanted elements from a list. Multiple elements deletion operation can be performed on the list if you have elements to be removed, indices of the elements, index range of the elements, etc. In this tutorial, you will learn all related solutions to remove multiple elements from a list.

Example

Here is an example with sample input/output values to understand the solution in a better way:

Input:
list1 = [10, 20, 30, 40, 50, 60, 70]
indices = 0, 2, 4
Output:
list1 = [20, 40, 60, 70]

Input:
list1 = [10, 20, 30, 40, 50, 60, 70]
indices = 1, 3
Output:
list1 = [10, 30, 50, 60, 70]

1. Remove multiple elements at specified indices

To remove multiple elements from a Python list based on the indices, you can use enumerate() function and list comprehension. In this way, all elements which are not on the given indices will be filtered, and then create a new list. Consider the below code statement,

indices = 0, 2, 4
list1 = [i for j, i in enumerate(list1) if j not in indices]

Example

# list
list1 = [10, 20, 30, 35, 45, 55, 10, 30, 45]

# printing original list
print("Original list: ", list1)

# Removing elements at specified indices
indices = 0, 2, 4
list1 = [i for j, i in enumerate(list1) if j not in indices]

# Print the updated list
print("List after removing elements:", list1)

Output

Original list:  [10, 20, 30, 35, 45, 55, 10, 30, 45]
List after removing elements: [20, 35, 55, 10, 30, 45]

2. Remove multiple elements by index range

To remove multiple elements from a Python list based on the given index range, you can use del keyword by specifying the start and end index of the list. In this way, all those elements which are on these indices will be removed. Consider the below code statement,

del list1[2:6]

Example

# list
list1 = [10, 20, 30, 35, 45, 55, 10, 30, 45]

# printing original list
print("Original list: ", list1)

# Remove elements by index range
# from index 2 to 6
del list1[2:6]

# Print the updated list
print("List after removing elements:", list1)

Output

Original list:  [10, 20, 30, 35, 45, 55, 10, 30, 45]
List after removing elements: [10, 20, 10, 30, 45]

3. Remove multiple specified elements

To remove multiple elements from a Python list based on given multiple values i.e., the given elements, create a list of elements to be removed and then remove the elements using the list.remove() method by iterating over the list. Consider the below code statements,

eleToRemove = [10, 45, 30]
for x in list1:
    if x in eleToRemove:
        list1.remove(x)

Example

# list
list1 = [10, 20, 30, 35, 45, 55, 10, 30, 45]

# printing original list
print("Original list: ", list1)

# Remove multiple specified elements
# Elements to remove
eleToRemove = [10, 45, 30]
for x in list1:
    if x in eleToRemove:
        list1.remove(x)

# Print the updated list
print("List after removing elements:", list1)

Output

Original list:  [10, 20, 30, 35, 45, 55, 10, 30, 45]
List after removing elements: [20, 35, 55, 30]

4. Remove multiple elements based on a calculation (divisible by N)

To remove multiple elements from a Python list based on a given calculation, you can use Python list comprehension by specifying the condition. This will filter list elements and returns a new list. Consider the below code statement,

N = 15
list1 = [x for x in list1 if x % N != 0]

Example

# list
list1 = [10, 20, 30, 35, 45, 55, 10, 30, 45]

# printing original list
print("Original list: ", list1)

# Remove multiple elements based on a
# calculation (divisible by N)
N = 15
list1 = [x for x in list1 if x % N != 0]

# Print the updated list
print("List after removing elements:", list1)

Output

Original list:  [10, 20, 30, 35, 45, 55, 10, 30, 45]
List after removing elements: [10, 20, 35, 55, 10]

5. Remove multiple occurrences of a specific element

To remove multiple elements (all occurrences of a given element) from a Python list, you can use Python list comprehension by specifying the condition. This will filter list elements excluding all occurrences of the given element, and returns a new list. Consider the below code statement,

Example

# list
list1 = [10, 20, 30, 35, 45, 55, 10, 30, 45]

# printing original list
print("Original list: ", list1)

# Remove multiple occurrences of a specific element
N = 10 # element to remove
list1 = [x for x in list1 if x != N]

# Print the updated list
print("List after removing elements:", list1)

Output

Original list:  [10, 20, 30, 35, 45, 55, 10, 30, 45]
List after removing elements: [20, 30, 35, 45, 55, 30, 45]

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.