How to use the del method in Python?
In Python, the del
statement is used to remove elements from a list, dictionary, or any other data structure.
The del
statement allows you to delete an element from a list by specifying its index or delete an entire list altogether.
# Creating a list of fruits
fruits = ['apple', 'banana', 'orange', 'grape']
# Removing the element at index 1 (banana) using del
del fruits[1]
Syntax
del list[index]
How to use del method in Python
# Creating a list of fruits
fruits = ['apple', 'banana', 'orange', 'grape']
# Removing the element at index 1 (banana) using del
del fruits[1]
print("Updated List:", fruits)
#output: Updated List: ['apple', 'orange', 'grape']