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.
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 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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to end a program in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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.
