How to remove items from a Python list?
There are several methods to delete or remove items from a Python list.
Remove a single item from the Python list with ‘del’
The delete function, ‘del’ can be used for collections, specifically, lists and dictionaries in Python. To remove a single element or a range of elements specify the list indexes.
letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
#Determine the index position of c
d = letters.index('c')
#Delete c from the list
del letters[d]
print(letters)
#Output ['P', 'r', 'o', 'u', 'r', 'e']
The del function simply updates the list, without returning any value. Therefore, use the print( ) for output.
Remove a range of items from a Python list
To delete a range of items from a Python list, the index range has to be mentioned as a list. This index range should start from the position of the first item to be deleted, till the index of the last item to be deleted.
letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
#Delete a range of items from the list
del letters[1:4]
print(letters)
#Output ['P', 'u', 'r', 'e']
Deleting a list
Lastly, using the del function, an entire list can also be deleted.
letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
del letters
print(letters)
#Output NameError: name 'letters' is not defined
The print function throws an error as the list ‘letters’ was deleted and do not exist anymore.
Remove items using the list.remove( ) function
To remove a specific element from an existing list, the list.remove(item) function can be used. This is different from the ‘del’ function, which removes an element present at a specific index. Whereas, the ‘remove’ function, removes the first matching value in the list.
letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
letters.remove('r')
print(letters)
#Output ['P', 'o', 'c', 'u', 'r', 'e']
Removing items by replacing the list with an empty list
Items of a Python list can also be removed, by replacing an index range with an empty list. In this case, the Python interpreter will update the items within the range from the new list.
letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
#Replacing items with an empty list
letters[1:4] = [ ]
print(letters)
#Output ['P', 'u', 'r', 'e']
Remove the list items by slicing
Slicing an entire list will remove all of the list items and an empty list will be generated.
letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
letters[:] = [ ]
print(letters)
#Output []
Here, the range defined has no indexes, suggesting that the function has to be implemented upon the entire list. By assigning the entire range of items to an empty list, Python clears the whole list.
Pop the list item
Pop refers to a combination of removing and returning items from a list. Using the list.pop( ) function, it is possible to remove a particular item from a list. In case the index is not provided, the Python interpreter will remove and return the last element. Popping is similar to the ‘del’ function but pop returns the removed element, whereas the ‘del’ function doesn’t.
letters = ['P', 'r', 'o', 'c', 'u', 'r', 'e']
print(letters.pop(5))
print(letters)
#Output r ['P', 'r', 'o', 'c', 'u', 'e']
I work as an editor and writer for Project Guru. I have a keen interest in new and upcoming learning and teaching methods. I have worked on numerous scholarly projects in the fields of management, marketing and humanities in the last 10 years. Currently, I am working in the footsteps of the National Education Policy of India to help and support fellow professors to emphasise interdisciplinary research and curriculum design.
I am a management graduate with specialisation in Marketing and Finance. I have over 12 years' experience in research and analysis. This includes fundamental and applied research in the domains of management and social sciences. I am well versed with academic research principles. Over the years i have developed a mastery in different types of data analysis on different applications like SPSS, Amos, and NVIVO. My expertise lies in inferring the findings and creating actionable strategies based on them.
Over the past decade I have also built a profile as a researcher on Project Guru's Knowledge Tank division. I have penned over 200 articles that have earned me 400+ citations so far. My Google Scholar profile can be accessed here.
I now consult university faculty through Faculty Development Programs (FDPs) on the latest developments in the field of research. I also guide individual researchers on how they can commercialise their inventions or research findings. Other developments im actively involved in at Project Guru include strengthening the "Publish" division as a bridge between industry and academia by bringing together experienced research persons, learners, and practitioners to collaboratively work on a common goal.
Discuss