Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference Between Del and Remove() on Lists in Python?
Python lists provide two primary ways to remove elements: the del keyword and the remove() method. Both serve different purposes and work differently based on whether you know the index or the value.
Del Keyword in Python List
The del keyword removes elements by index position. It can delete single elements, multiple elements using slicing, or the entire list ?
Delete a Single Element
# Create a List
car_brands = ["Toyota", "Benz", "Audi", "Bentley"]
print("List =", car_brands)
# Delete element at index 2
del car_brands[2]
print("Updated List =", car_brands)
List = ['Toyota', 'Benz', 'Audi', 'Bentley'] Updated List = ['Toyota', 'Benz', 'Bentley']
Delete Multiple Elements
# Create a List
car_brands = ["Toyota", "Benz", "Audi", "Bentley", "Hyundai", "Honda", "Tata"]
print("List =", car_brands)
# Delete multiple elements using slice
del car_brands[2:5]
print("Updated List =", car_brands)
List = ['Toyota', 'Benz', 'Audi', 'Bentley', 'Hyundai', 'Honda', 'Tata'] Updated List = ['Toyota', 'Benz', 'Honda', 'Tata']
Delete Entire List
# Create a List
car_brands = ["Toyota", "Benz", "Audi", "Bentley"]
print("List =", car_brands)
# Deletes the entire list variable
del car_brands
# This would raise NameError since car_brands no longer exists
# print(car_brands)
Remove() Method in Python List
The remove() method removes the first occurrence of a specific value from the list ?
Remove by Value
# Create a List
car_brands = ["Toyota", "Benz", "Audi", "Bentley"]
print("List =", car_brands)
# Remove by value
car_brands.remove("Benz")
print("Updated List =", car_brands)
List = ['Toyota', 'Benz', 'Audi', 'Bentley'] Updated List = ['Toyota', 'Audi', 'Bentley']
Remove First Occurrence Only
# List with duplicate values
numbers = [1, 2, 3, 2, 4, 2]
print("Original List =", numbers)
# Remove first occurrence of 2
numbers.remove(2)
print("After remove(2) =", numbers)
Original List = [1, 2, 3, 2, 4, 2] After remove(2) = [1, 3, 2, 4, 2]
Key Differences
| Feature | del | remove() |
|---|---|---|
| Type | Python keyword | Built-in list method |
| Works with | Index position | Element value |
| Error when not found | IndexError | ValueError |
| Multiple deletions | Can delete ranges with slicing | Only removes first occurrence |
| Performance | Direct deletion by index | Searches list first, then removes |
| Can delete entire list | Yes | No |
When to Use Which?
Use del when you know the exact index position or want to delete multiple elements. Use remove() when you know the value but not its position ?
fruits = ["apple", "banana", "orange", "banana"]
# Use del when you know the index
del fruits[0] # Removes first element
print("After del fruits[0]:", fruits)
# Use remove() when you know the value
fruits.remove("banana") # Removes first "banana"
print("After remove('banana'):", fruits)
After del fruits[0]: ['banana', 'orange', 'banana']
After remove('banana'): ['orange', 'banana']
Conclusion
Use del for index-based deletion and when you need to remove multiple elements or entire lists. Use remove() for value-based deletion when you don't know the index position.
