Python List Deletion Techniques: Using pop, remove, and clear (Day 81)
This tutorial demonstrates how to delete elements from Python lists by using the pop() method with optional index, the remove() method for specific values, and the clear() method to empty the list, illustrated with concrete examples and output results.
Day 81 of the Python learning notes introduces common list deletion operations.
The pop() method removes an element by its index; if no index is provided it removes the last element. Example:
list1 = ['西游记', '三国演义', '红楼梦', '水浒传']
list1.pop(1)
print(list1) # ['西游记', '红楼梦', '水浒传']
list1.pop()
print(list1) # ['西游记', '红楼梦']The remove() method deletes the first occurrence of a specified value. Example:
list1.remove('红楼梦')
print(list1) # ['西游记']The clear() method empties the entire list:
list1.clear()
print(list1) # []These examples show the practical differences between index‑based removal, value‑based removal, and full list clearing.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
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.
