Master Python List Deletion: remove, pop, and del Explained
Learn how to delete elements from Python lists using three primary methods—remove for value-based removal, pop for index-based removal that returns the element, and del for deleting by index, range, or entire objects—complete with clear code examples and key usage notes.
Introduction
In Python, there are three common ways to delete elements from a list: remove, pop, and del.
1. remove
Deletes a single element by value, removing the first occurrence that matches.
>> str = [1, 2, 3, 4, 5, 2, 6]
>>> str.remove(2)
>>> str
[1, 3, 4, 5, 2, 6]2. pop
Deletes an element by index and returns the removed element. It can also be used on any indexable object.
>> str = [0, 1, 2, 3, 4, 5, 6]
>>> str.pop(1) # pop returns the removed element
>>> str
[0, 2, 3, 4, 5, 6]
>>> str2 = ['abc', 'bcd', 'dce']
>>> str2.pop(2)
'dce'
>>> str2
['abc', 'bcd']3. del
Deletes elements based on their index. It can remove a single element, a slice, or an entire object.
>> str = [1, 2, 3, 4, 5, 2, 6]
>>> del str[1]
>>> str
[1, 3, 4, 5, 2, 6]
>>> str2 = ['abc', 'bcd', 'dce']
>>> del str2[1]
>>> str2
['abc', 'dce']
>>> # Delete a range (slice)
>>> str = [0, 1, 2, 3, 4, 5, 6]
>>> del str[2:4] # removes elements at index 2 up to but not including 4
>>> str
[0, 1, 4, 5, 6]
>>> # Delete the entire list object
>>> str = [0, 1, 2, 3, 4, 5, 6]
>>> del str
>>> str
NameError: name 'str' is not definedNote: del removes the reference (variable) to an object, not the object itself; the actual data is reclaimed by Python's garbage collector.
Alternative Approach
A different way to “delete” elements is to build a new collection that excludes unwanted items.
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)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
