Remove all occurrences of an element from a Python list

Removing all occurrences of a list element: In this tutorial, we will learn how to remove all occurrences of a given element in the Python list. Learn with logic and examples. By IncludeHelp Last updated : August 17, 2023

Problem statement

Given a Python list, we have to remove all occurrences of the given elements from it.

Remove all occurrences of a given element

Removing an element can be done using the list.remove() method. But when you want to remove all occurrences of a given element, you need to iterate over the list elements and remove all occurrences of that element by using the list.remove() method.

Example

Consider the below example without sample input and output:

Input:
list = [10, 20, 10, 30, 10, 40, 10, 50]
n = 10

Output:
list after removing 10 from the list
list = [20, 30, 40, 50]

Method 1: Iterating over the list using for ... in and remove() method

Logic

  • Declare a list and initialize it with elements.
  • Declare a number (n) to be deleted from the list.
  • Iterate over the list using the for … in loop.
  • Check each element whether it is equal to the number (n). If the condition is True then remove the element using the remove() method.

Program

# list with integer elements
list = [10, 20, 10, 30, 10, 40, 10, 50]
# number (n) to be removed
n = 10

# print original list 
print ("Original list:")
print (list)

# loop to traverse each element in list
# and, remove elements 
# which are equals to n
for x in list:
    if x == n:
        list.remove(x)

# print list after removing given element
print ("list after removing elements:")
print (list)

Output

Original list:
[10, 20, 10, 30, 10, 40, 10, 50]
list after removing elements:
[20, 30, 40, 50]

Method 2: Iterating over the list using while loop and remove() method

Logic

  • Run a while loop from 0th element to last element's index.
  • Check the element whether it is equal to the number (which is to be removed) or not.
  • If any element of the list is equal to the number (which is to be removed), remove that element from the list.
  • To remove the number from the list, use list.remove() method.
  • After removing the number/element from the list, decrease the length, because one item is deleted, and then continue the loop to check the next item at same index (because after removing the element – next elements shifts to the previous index.
  • If element is not found (i.e. is not removed), then increase the loop counter to check next element.

Program

# list with integer elements
list = [10, 20, 10, 30, 10, 40, 10, 50]

# number (n) to be removed
n = 10

# print original list
print("Original list:")
print(list)

# loop to traverse each element in list
# and, remove elements
# which are equals to n
i = 0  # loop counter
length = len(list)  # list length
while i < length:
    if list[i] == n:
        list.remove(list[i])
        # as an element is removed
        # so decrease the length by 1
        length = length - 1
        # run loop again to check element
        # at same index, when item removed
        # next item will shift to the left
        continue
    i = i + 1

# print list after removing given element
print("list after removing elements:")
print(list)

Output

Original list:
[10, 20, 10, 30, 10, 40, 10, 50]
list after removing elements:
[20, 30, 40, 50]

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





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