Python is a high-level programming language. It is one of the most popular and widely used programming languages. Python comes with many features. And it’s syntax is very simple. One of the most commonly used features of python is its collections. In this article, we will show how to remove items from a list collection in Python. There are four types of collections that we can use to store data.
Topics in this article
- List collection in python
- Remove item from the list
- The remove method
- The del keyword
- The pop method
- The clear method
- Conclusion
1. List: An ordered and changeable collection.
2. Tuple: An ordered and unchangeable collection.
3. Set: An unordered and unindexed collection.
4. Dictionary: An unordered, changeable and indexed collection.
List and tuple allow duplicate values while the set and dictionary do not allow duplicate values. Out of these four, the List of the most widely used collection and in this article, we learn more about the list and how to remove items from it.
List collection in python
A list collection in python that is used to store any kind of data. A list in python is ordered and we can change it according to our requirements. Square brackets are used for writing lists in python.
firstList = [1,2,3,4,5]
We created a list containing five numbers. We can add any type of values in a list. The following python code has a list containing values of different types.
firstList = ["Python", 100, true]
We can perform various operations on a list in python. In this article, we will focus on the ways of removing items from a list.
Remove item from a list
There are various ways of removing items from a list. We can remove specific items from a list, or we can remove the last items from a list. We can also remove everything from a list or delete the entire list. Let’s discuss these ways with examples.
The remove method
The most common way of removing an item from a list is by using the remove method. We can remove a specific item from a list using this method.
snakeList = ["Python", "Cobra", "Anaconda", "Boa", "Mamba"] snakeList.remove("Cobra") print(snakeList)
The output is:
In the above python code, we created a list named snakeList and added the names of five snakes in it. Further, we used the remove method and passed the name of the second item, i.e. Cobra. And the Cobra is removed from the snakeList.
The del keyword
The del keyword is another common way of removing items from a list. What if we want to remove an item from the list using its index? The del keyword can the job. Let’s understand the del keyword with an example.
snakeList = ["Python", "Cobra", "Anaconda", "Boa", "Mamba"] del snakeList[1] print(snakeList)
The output is:
We used the same snakeList and this time again, we will remove Cobra from it. This time by using its index. The item we want to remove is on the first index(because index in a list starts from zero). We used the del keyword followed by the name of the list. The index is written between square brackets. This is how items are removed by using the del keyword.
Apart from removing an item, the del keyword is also used to delete the list completely.
snakeList = ["Python", "Cobra", "Anaconda", "Boa", "Mamba"] del snakeList
To delete the snakeList completely, we used the del keyword followed by the name of the list. The snakeList does not exist anymore.
The pop method
The remove method and del keyword are used to remove any specific item from the list. But pop works differently. We use the pop method to remove the last item from the list.
snakeList = ["Python", "Cobra", "Anaconda", "Boa", "Mamba"] snakeList.pop() print(snakeList)
The output is:
We used the pop method to remove the last item from the snakeList. The item, Mamba, does not exist anymore in the snakeList. Basically, we use the pop method to remove the last item of the list, but we can also use it in one other way.
We can also use the pop method to remove an item by using its index as we did with the del keyword. The only difference is, the pop method returns the removed item. Let’s verify it.
snakeList = ["Python", "Cobra", "Anaconda", "Boa", "Mamba"] removedItem = snakeList.pop(1) print(removedItem)
We used the pop method to remove the item at the first index of the snakeList by passing one in the pop method. This time, we used a variable, removedItem to store the returned value from the pop method. And what is the returned value? The same item that was removed from the list.
The clear method
We discussed how to remove items from a list by using various methods such as remove, pop and del keyword. We also learned how to delete a list completely. But what if we want to empty a list? An empty list means, a list with no data. We can use the clear method for it.
snakeList = ["Python", "Cobra", "Anaconda", "Boa", "Mamba"] snakeList.clear() print(snakeList)
The snakeList contains the names of five snakes. We used the clear method to empty the snakeList. Now, do not confuse it with the working of the del keyword. The del keyword deletes the list completely from the memory, but the clear method only empties it. The list still exists but without any data.
Conclusion: how to remove items from a list in Python
The list collection is one of the most simple ways of storing data. We can perform insertion and deletion very easily. In this article, we discussed the list collection and different ways of removing items from a list in Python. The remove method is one common and simple way. Other ways such as the pop method and del keywords are also helpful. We also discussed the functioning of the del keyword to delete a list completely and, the clear method to empty a list.