Overview of Python list remove()
List in python is a pre-defined method used for performing operations on a group/ collection of items. Though it is a similar construct to an array, the list function is heterogeneous, unlike the array function, contributing to its powerful accessibility in python. This list function allows many operations like insert, append, sum, count, extend, index, etc. One such operation is remove(), which is used for removing or deleting an item from the list.
The list is a similar concept to arrays. However, the only difference between both is that the array is always homogeneous while the list can be heterogeneous. This feature of the list makes it stand powerful in Python.
List_example = ['Mathematics', 'biology', 1997,'Hello123'] print(List_example)
The list always starts with index 0. Each element in the list holds its space. The list can have duplicate values in it, as the index would be different for both. The list has many methods to perform various operations related to it. Like:
Insert, extend, append, sum, count, index, remove, etc.
Let us discuss one important method here:
remove(): The method remove() is to delete elements from a list.
Examples of Python list remove()
Let’s get the concept clear through some examples:
List_example = [2.34, 2.3, 3.4, 4.1, 1.054] List_example.remove(3.4) print(List_example)
As one can notice, we defined a list and then, with the help of the method, remove() we removed element 3.4 from the list. The element that needs to be removed has to go as an argument to those methods. Here we removed only one element. However, one must be wondering: what if we need to remove more than one element.
In that case, remove() doesn’t work in one go. The other way of doing it could be:
List_example = [2.34, 2.3, 3.4, 4.1, 1.054,6,7,8] List_example.remove(6) List_example.remove(7) List_example.remove(8) print(List_example)
If there are few elements to be removed from the list, it could be done one by one(as shown above). But if it’s more elements, then this way of doing it could be tedious and unprofessional.
item_list_ex = ['dog', 5, 'cat', 8.9, True] print(item_list_ex) item_list_ex = [x for x in item_list_ex if x not in ('dog', 5)] print(item_list_ex)
This way is best to remove elements, as you need to mention all elements to be removed in one shot(as shown in the above logic).
Let’s see an example with try and catch block.
a = ['Heena', 10, 'Rohit', 80, True] try: a.remove(90) except ValueError: print("Doesn't exist") pass # do nothing!
As one can notice, element 90 is not part of the list. Hence, while executing a.remove(90), it has thrown ValueError. Henceforth, “Doesn’t exist” gets printed.
If there is a sequence of numbers, and you want to remove the range of elements from it. It can be done with the help of for loop. See the example shown below:
List_example = [1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12] print("Present List: ") print(List_example) for i in range(5, 8): print(i) List_example.remove(i) print("\nList after Removing a range of elements: ") print(List_example)
One can see, from a list of 1 to 12, three elements have been removed with the help of range, for loop and remove function.
A list can hold duplicate values, so how does it remove() work in that case? Let’s explore the answer to that.
List_example = [2.34, 2.3, 3.4, 4.1, 1.054,6,2.3,7,8,2.3] print("Present List: ",List_example) List_example.remove(2.3) print("Present List: ",List_example)
Method remove() only removes the first matching element from the list. As shown in the example, there are three values of 2.3. When we removed it, two still the same available on the list.
The simple answer would be, remove() is a value-based method and hence can remove the element from any place of the list by specifying its value. It could first, last or in between. However, the pop() method removes the last element(that’s by default). But if elements need to be removed from any position, its index has to be specified. In other words, pop() is an index-based removal of elements from the list.
Highlighting difference between remove() and pop():
List_example = [1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12] print("Present List: ",List_example) List_example.remove(3) print("Present List: ",List_example) List_example.pop(3) print("Present List: ",List_example)
As one can notice, remove(3) removed value 3 from the list. However, pop(3) removed the element at the 3rd position, which is 5. The list always starts from index 0; hence 3rd element of a list is value 5.
The advantage of using a list lies in the fact that one can easily iterate over it. as the sequence of elements is easy to handle here.
Conclusion – Python list remove()
The above covered is one of the most important and basic methods which is widely used while handling the list in python. Then we saw the difference between pop and remove, which is a usual confusing parameter while handling the list. This topic has given enough highlights about the list remove(). One can go through other methods of the list to get well versed with the list concept. Don’t forget, Collection is one of the widely used concepts in python. The more one knows about the methods, the handier it becomes to write and understand the python code.
This is a guide to the Python list remove(). Here we discuss the overview, Examples of Python list remove(), and the codes & outputs. You may also look at the following articles to learn more –