How to remove items from a Python list?

By Abhinash & Priya Chetty on August 19, 2022

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']

Priya is the co-founder and Managing Partner of Project Guru, a research and analytics firm based in Gurgaon. She is responsible for the human resource planning and operations functions. Her expertise in analytics has been used in a number of service-based industries like education and financial services.

Her foundational educational is from St. Xaviers High School (Mumbai). She also holds MBA degree in Marketing and Finance from the Indian Institute of Planning and Management, Delhi (2008).

Some of the notable projects she has worked on include:

  • Using systems thinking to improve sustainability in operations: A study carried out in Malaysia in partnership with Universiti Kuala Lumpur.
  • Assessing customer satisfaction with in-house doctors of Jiva Ayurveda (a project executed for the company)
  • Predicting the potential impact of green hydrogen microgirds (A project executed for the Government of South Africa)

She is a key contributor to the in-house research platform Knowledge Tank.

She currently holds over 300 citations from her contributions to the platform.

She has also been a guest speaker at various institutes such as JIMS (Delhi), BPIT (Delhi), and SVU (Tirupati).

 

I am currently working as a Research Associate. My work is centered on Macroeconomics with modern econometric approach. Broadly, the methodological research focuses on Panel data and Times series data analysis for causal inference and prediction. I also served as a reviewer to Journals of Taylor & Francis Group, Emerald, Sage.

Discuss