Python list del: How to remove or delete an element from a list
Python built-in function del
deletes the element of a list.
a = ['Book', 'Note', 'Pen']
del a[1]
print(a) # ['Book', 'Pen']
The element of a
, whose index is 1, is removed. This function can remove items in a row.
a = ['Book', 'Note', 'Pen']
del a[0], a[1]
print(a) # ['Note']
['Book', 'Note', 'Pen']
-> [Note', 'Pen']
-> [Note']
del
raises the exception if the index is out of range.
a = ['Book', 'Note', 'Pen']
del a[4]
# IndexError: list assignment index out of range
Python List
- Get the number of elements
- Append an element
- Extend a list
- Reverse a list
- Sort a list
- Shuffle a list
- Get the index of an element
- Pop an element
- Delete an element from a list
- Clear a list
- Empty list
- Python range
- Get the range of integers
- Get a random element from a list
- Join a list
- Get keys and values iterating a list
- List comprehension
- Get the last element
- Insert an object at the first position
- Get the index of the max value
- Convert a list to a tuple
- Convert a list to json
- Filter
- Iterate lists in parallel
- Generator
- Make a cartesian product
- Sort a list of tuples by the first or second element