How to remove items from a list in Python – with example

  • Reading time:15 mins read

How to remove items from a list in Python?

Lists are some of the commonly used among the four built-in data types in Python.

Lists are created using square brackets and are used to store multiple values in a single variable. In addition to this lists are changeable, allow duplicate values, can be indexed and are ordered.

When working with lists we may want to remove some items for various reasons. In Python, there are three different ways that we can use to remove members of a list. Now depending on our needs, we may choose one of the methods.

In this article, we are going to explore examples that will allow us to learn how to remove items from a list in Python with these methods and the distinctions between them.

 

 

How to remove items from a list in Python with the remove() method

The remove() method is used to remove the first occurrence of the element specified as an argument within the parentheses. 

This means that if we have an element occurring more than once in a list this method will only remove the first matching member of that list. For example, in the list that we created above, we have the item ‘lion’ occurring twice in the list. 

 

remove items from a list in python

 

Now remove the item ‘lion’ from the list we are first going to first print the original list using a print statement. Now using the remove() method, we are going to remove the item ‘lion’ from the list and also print the list after we have removed this item as shown below.

So as we can see in the example above the first instance of the item ‘lion’ has been removed from the list.

 

 

How to remove items from a list in Python with the del keyword

In Python, the keyword del is used to delete objects and also to delete items from iterables such as lists as a given index. An index number on the other hand refers to the position of an item within an ordered list. 

Now Python, like most languages, follows zero-based numbering where the initial element in a sequence such as a list is assigned to index 0. Therefore, in the list that we initially created the item ‘elephant’ is at index 0, while ‘lion’ and ‘giraffe’ would be at index 1 and 2 respectively.

Now remove the first item in the list we will first begin with the keyword del, followed by the name of the list and square brackets and index zero. In this case index, zero refers to the first item in the list.

 

 

Using this method we have been able to remove the first item from the list, in this case, we only need to have the correct index of the item that we want to remove. On the other hand, when using the remove() method we need to know the actual value of the item that we want to remove.

 

How to remove items from a list in Python

 

How to remove items from a list in Python with the pop() method

The pop() method removes an item at the specified index from the list and returns the item.

Unlike the methods that we have discussed above,  this method returns the item that we have removed from the list. This is useful if you want to take something out of the list and hold on to that information and do something else with it.

Using this method we are going to remove the item at index 3 and store the result in a variable that we will name popped_item.  Using the print function we have then printed out the item that we have removed from the list.

 

 

Summary

So this is how you can remove items from a list in Python. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.