Python remove element from list [Practical Examples]


Python

Author: Bashir Alam
Reviewer: Deepak Prasad

 

Introduction to Python remove element from list

A list is an ordered and mutable Python container, being one of the most common data structures in Python. To create a list, the elements are placed inside square brackets ([]), separated by commas. In this tutorial, we will learn how we can remove the elements from a list using the Python programming language.

We will use multiple ways to remove elements from a list including remove() method, pop() method, clear() method, and del keyword using various examples. Moreover, we will also learn how we can remove duplicate elements and the elements that meet a certain condition. In a nutshell, this tutorial contains all the possible ways to remove the elements from the python list.

 

Getting started with Python remove element from list

Before learning how we can remove elements from the python list, let us first recap how we can create a list in Python. You can read more about lists from the article on Python lists (Please link to Python list here if any). See the example below where we had created a python list and printed it.

# defining python list of integers
my_Int_List = [1, 2, 3, 4, 5, 6]
# defining python list of strings
my_str_list = ["a", "b", "c", "d"]
# defining python list of float
my_float_list = [2.33, 4.543, 6.2345]
# printing the lists
print(my_Int_List)
print(my_str_list)
print(my_float_list)

Output:

[1, 2, 3, 4, 5, 6]
['a', 'b', 'c', 'd']
[2.33, 4.543, 6.2345]

Notice that in the example above, we have created different lists that contain different data typed elements and then we printed those lists. In the following sections, we will discuss how we can remove the elements from the list in various ways.

 

Python remove element from list using remove() method

Python List type provides the remove() method as a built-in method to remove items from the current list. The remove() method provides very simple usage where the item or object we want to remove is provided to the remove() method as a parameter. The following is the simple syntax of the python remove() method.

list.remove(element)

The remove() method takes the element or item of list as an argument and removes that element from the list.

 

Example-1 Remove single element from list

Now let us take an example and see how we can remove an element from a list using this method. In the following example, we have a list of students and let say we want to remove 'Bashir' from the list.

# defining list 
mylist = ['alam', 'elran', 'Bashir', 'KD']
# Python remove element from list
mylist.remove('Bashir')
# printing
print(mylist)

Output:

['alam', 'elran', 'KD']

Notice that in the output the student name 'Bashir' has been removed because we provided it as an argument to the remove() method.

 

Example-2 Remove multiple elements from a list

We can use Python remove() method to remove all duplicate elements from a list. See the following example where we use the Python while loop and remove() method to remove all the duplicate elements from a list.

# defining list 
mylist = [1, 2, 5, 4, 5, 5, 7, 5, 8, 5]
# for loop to iterate through list
while 5 in mylist:
    # python remove element from list
    mylist.remove(5)
# print
print(mylist)

Output:

[1, 2, 4, 7, 8]

So the while loop will be executed unless there will be no 5 in the list, and we are using the python remove() method inside the while loop to remove the element each time the loop is executed.

 

Example-3 Remove element from list that does not exist

Now let us use the same method to remove an element from a list that does not exist. See the following example where we are trying to remove the number 10 from the list.

# defining list 
mylist = [1, 2,3,4,5,6,7,8,9]
# Python remove element that does not exist
mylist.remove(10)
# print
print(mylist)

Output:

Python remove elements from list

Notice that we get an error that says that the provided argument is not in the list.

 

Python remove element from list using clear() method

Python clear() method removes all the elements from the list. It clears the list completely and returns nothing. The following is the simple syntax of the Python clear() method.

list.clear()

It does not require any parameter and returns no exception if the list is already empty. In this section, we will take examples and see how clear() method works.

 

Example of using clear() method

As we already said, the clear method removes all the elements from the list at once and returns an empty list. See the example below:

# defining list 
mylist = [1, 2,3,4,5,6,7,8,9]
# Python remove element from list using clear method
mylist.clear()
# printing
print(mylist)

Output:

[]

Notice that our list becomes empty because of the clear method which removes all the elements at once. If we will apply the same method on an empty list, then it will return the same list without any error. See the example below:

# defining list 
mylist = []
# Python remove element from list using clear method
mylist.clear()
# printing
print(mylist)

Output:

[]

So, when we apply the clear method on the empty list, it will return the same empty list without any error.

 

Python remove elements from list using pop() method

The Python pop() method removes an item at a specified index value from a list. It takes a single argument, which is the index of the List. The argument passed to a pop() method is optional. If we will not pass the argument, then the default index -1 will be passed as an argument. If the given index is not in range, it throws IndexError. The following is the simple syntax of the Python pop() method.

list.pop(index)

This method can take only one optional argument which is the position of the element.

 

Example of using pop() method

Now let us take an example and see how the Python pop method works. See the Python program below:

# defining list 
mylist = [1, 2,3,4,5,6,7,8,9]
# Python remove element from list using pop method
mylist.pop(2)
# printing
print(mylist)

Output:

[1, 2, 4, 5, 6, 7, 8, 9]

Notice that the element on index 2 which was 3 has been removed from the list. If we will not specify the index position, then by default the last element will be removed. See the example below:

# defining list 
mylist = [1, 2,3,4,5,6,7,8,9]
# Python remove element from list using pop method
mylist.pop()
# printing
print(mylist)

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

Notice that we did not has provided the index position and by default, the last element from the list has been removed.

 

Python remove element from list using del keyword

The del keyword in python is primarily used to delete objects in Python. Since everything in python represents an object in one way or another, The del keyword can also be used to delete a list, slice a list, delete dictionaries, remove key-value pairs from a dictionary, delete variables, etc. The following is the simple syntax of removing elements from a list using the del keyword.

del list_name[index]

If we will not specify the index position of a list, then instead of removing element, the del keyword will remove the whole list.

 

Example-1 Remove single element from list

Now let us take an example and see how we can use the del keyword to remove an element from a list. See the example below:

# defining list 
mylist = [1, 2,3,4,5,6,7,8,9]
# Python remove element from list using del keyword
del mylist[3]
# printing
print(mylist)

Output:

[1, 2, 3, 5, 6, 7, 8, 9]

Notice that the element on index 3 has been removed by the del keyword.

 

Example-2 Remove multiple elements from a list

We can also remove multiple elements at a time from a list using the del keyword. We have to first specify the elements index positions that we want to remove or in simple words, we have to slice our list and remove the sliced part using the del keyword. See the example below:

# defining list 
mylist = [1, 2,3,4,5,6,7,8,9]
# Python remove element from list using del keyword
del mylist[3:6]
# printing
print(mylist)

Output:

[1, 2, 3, 7, 8, 9]

Notice that we slice our list and remove the sliced part. All the elements from index position 3 up to 6 have been removed.

 

Example-3 Remove the whole list

As we already discussed that if we will not specify the index position, then the del keyword will remove the whole list from our program. See the Python program below:

# defining list 
mylist = [1, 2,3,4,5,6,7,8,9]
# Python remove element from list using del keyword
del mylist
# printing
print(mylist)

Output:

Python remove element from list

Although we have defined the list at the beginning of our program, then we used the del keyword to remove the list and it deleted the whole list from our program.

 

Python remove element from list that meets a certain condition

Removing items that satisfy the condition is equivalent to extracting items that do not satisfy the condition. And that is why the List comprehensions are used. You can read more about list comprehension from the article on Python List Comprehension. Now let us take an example and see how we can remove the elements that meet a certain condition.

# defining list 
mylist = [1, 2,3,4,5,6,7,8,9]
# Python remove element from list using list comprehension
print('Remove all the odd elements:')
print([i for i in mylist if i % 2 == 0])

Output:

Remove all the odd elements:
[2, 4, 6, 8]

Notice that we had successfully removed all the odd elements from our list using the list comprehension method.

 

Summary

In this tutorial, we learned about the Python remove element from list using various methods. We discussed some of the build-in methods that are used to remove elements from a list. For example, Python remove() method, pop() and clear() method. At the same time, we learned how we can remove multiple elements that meet a certain condition by using the list comprehension method. Moreover, we will discuss the del keyword, which is used to delete objects in Python. We solved various examples of removing elements and lists from our program using the del keyword.

 

Further Reading

Python list documentation
Python list remove() method
Python list pop() method

 

Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment