all Python 3Lists (list)term
del for a list
removal
The del statement removes an item by index or a slice, changing the list in place; del lst removes only the name, not the object.
Syntax
del lst[i] | del lst[a:b] | del lst
del lst[i] and lst.pop(i) remove the same item, but del returns nothing — use pop when you still need the value. del lst[a:b] cuts the list in place, so every other name bound to that same list sees the change, whereas lst = lst[:a] + lst[b:] just builds a new object and rebinds the name. A bare del lst only removes the name: the object survives as long as other references point to it.
Examples
lst = [0, 1, 2, 3, 4]
del lst[2]
print(lst)
# → [0, 1, 3, 4]
del lst[1:3]
print(lst)
# → [0, 4]
lst2 = list(range(6))
del lst2[::2]
print(lst2)
# → [1, 3, 5]
lst3 = [1, 2, 3]
del lst3[:]
print(lst3)
# → [] (очистка через срез)
lst4 = [1, 2, 3]
del lst4
# NameError при обращении к lst4 после del
See also
Open in the full glossarydocs.python.orgReport an inaccuracy