How to Delete Elements from a Python List Using remove, pop, and del
This article explains three common Python techniques—remove, pop, and del—for deleting list elements, illustrating each method with clear code examples, usage notes, and important differences such as value‑based versus index‑based removal and reference handling.
In Python, there are three primary ways to delete elements from a list: remove , pop , and the del statement.
remove : deletes the first element that matches a given value. Example:
>> str = [1, 2, 3, 4, 5, 2, 6]
>>> str.remove(2)
>>> str
[1, 3, 4, 5, 2, 6]pop : deletes an element by its index and returns the removed value. Example:
>> str = [0, 1, 2, 3, 4, 5, 6]
>>> removed = str.pop(1) # returns 1
>>> str
[0, 2, 3, 4, 5, 6]
>>> str2 = ['abc', 'bcd', 'dce']
>>> str2.pop(2)
'dce'
>>> str2
['abc', 'bcd']del : deletes elements by index, slice, or the entire variable. Examples:
>> str = [1, 2, 3, 4, 5, 2, 6]
>>> del str[1]
>>> str
[1, 3, 4, 5, 2, 6]
>>> str = [0, 1, 2, 3, 4, 5, 6]
>>> del str[2:4] # removes elements at positions 2 and 3
>>> str
[0, 1, 4, 5, 6]
>>> del str
>>> str
NameError: name 'str' is not definedNote: del removes the reference (the variable name), not the underlying object; the object is reclaimed by Python’s garbage collector.
Alternative approach: create a new list that excludes unwanted values using tuple membership checks.
s1 = (1, 2, 3, 4, 5, 6)
s2 = (2, 3, 5)
s3 = []
for i in s1:
if i not in s2:
s3.append(i)
print('s1_1:', s1)
s1 = s3
print('s2:', s2)
print('s3:', s3)
print('s1_2:', s1)Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.