Signup/Sign In

Remove multiple elements from a list in Python

In this tutorial, you will learn to remove multiple elements from a list in Python. The List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index. We will be discussing few in-built functions which can be used to remove elements from a list.

Elements in a list can be deleted based on certain conditions or by specifying the range of their indexes.

First, we will see how to delete elements based on a condition.

Condition: Numbers that are divisible by 5 should be deleted from the list.

For example, given a list of numbers, you have to remove all those elements from the list which are divisible by 5.

Input: [10, 3, 12, 15, 5, 8]

Output: [3, 12, 8]

To solve this problem, we can follow these approaches:

  • For removing multiple elements from a list based on a certain condition, we can follow these approaches-
  1. Remove multiple elements from a list while Iterating.
  2. Remove multiple elements from a list using List Comprehension.
  • For removing multiple elements from a list by index range we can use del operator.

Approach 1: Iterating

In this approach, we will iterate over the list and remove them one by one if it is divisible by 5. We will use the remove() function to remove the particular number.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Take input of list

Step 2- Traverse the list and check each element if it is a multiple of 5

Step 3- Remove elements which are multiple of 5 from the list

Step 4- Print the list after removing elements

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. In this program, we have taken an input of elements of the list from the user and by using append(), added the elements to the list. The append() is a built-in function in the Python library.

li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
    e=int(input("Enter element of list "))
    li.append(e)

for i in list(li):
    if i % 5 == 0:
        li.remove(i)
print("List after removing elements ",li)


Enter size of list 5
Enter element of list 3
Enter element of list 8
Enter element of list 5
Enter element of list 1
Enter element of list 10
List after removing elements [3, 8, 1]

Approach 2: List Comprehension

In this approach, we will use the list comprehension method. List comprehension is a shorter syntax for creating a new list based on the values of an existing list. List comprehension will create a new list having all the numbers which are not divisible by 5 then it will copy this list in the original list. Now, the numbers which are divisible by 5 are removed from the list.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Take input of list

Step 2- Use list comprehension to add those numbers to the list which are not divisible by 5

Step 3- Print the new list

Python Program 2

In this program, we have taken an input of elements of the list from the user and using append() which is a built-in function in the Python library, added the elements to the list. To print the numbers with space, we have used end=" ".

li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
    e=int(input("Enter element of list "))
    li.append(e)

li=[num for num in li if num%5!=0]

print("List after removing elements ",li)


Enter size of list 6
Enter element of list 3
Enter element of list 6
Enter element of list 10
Enter element of list 12
Enter element of list 15
Enter element of list 5
List after removing elements [3, 6, 12]

Remove Multiple elements from list by index range using del

In this approach, we will use the del keyword to delete elements of a specified index range from the list.

This function will delete all the elements from index1 to index2-1.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Take input of list

Step 2- Remove list elements from list using del

Step 3- Specify index range

Step 4- Print the list after removing elements

Python Program 3

Follow the program to delete elements in a given range.

li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
    e=int(input("Enter element of list "))
    li.append(e)
print("Original list",li)
# removing from range
del li[1:4]

print("List after removing elements from range 1 to 4 ",li)


Enter size of list 6
Enter element of list 3
Enter element of list 6
Enter element of list 1
Enter element of list 2
Enter element of list 18
Enter element of list 6
Original list [3, 6, 1, 2, 18, 6]
List after removing elements from range 1 to 4 [3, 18, 6]

Conclusion

In this tutorial, we have discussed how to remove elements from a list based on certain conditions and elements in a given range. For deleting elements based on a condition we have learned how to use iteration and list comprehension methods. For deleting elements in a given range we have used the del keyword.