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
How to remove index list from another list in python?
In Python, removing multiple elements from a list using their indices requires careful handling to avoid index shifting issues. This article covers two effective methods: pop() and del keyword.
Using pop() method
Using del keyword
The key principle is to remove elements in descending order of their indices to prevent index shifting issues.
Why Sort Indices in Descending Order?
When removing elements from a list, removing from the beginning changes the indices of subsequent elements. Here's why descending order is crucial ?
Example of Index Shifting Problem
# Original list and indices to remove
items = ["Welcome", "to", "tutorialspoint", "python"]
indices_to_remove = [0, 2]
print("Original list:", items)
print("Indices to remove:", indices_to_remove)
print("Elements to remove:", [items[i] for i in indices_to_remove])
Original list: ['Welcome', 'to', 'tutorialspoint', 'python'] Indices to remove: [0, 2] Elements to remove: ['Welcome', 'tutorialspoint']
If we remove index 0 first, "to" shifts to index 0, and "tutorialspoint" shifts to index 1. Then removing index 2 would incorrectly remove "python" instead of "tutorialspoint".
Method 1: Using pop() Function
The pop() method removes and returns an element at a specific index ?
# Input list
input_list = ["Welcome", 20, "to", "tutorialspoint", 30, "python"]
print("Original list:", input_list)
# Indices to remove
given_indices = [1, 4, 5]
print("Indices to remove:", given_indices)
# Sort indices in descending order
sorted_indices = sorted(given_indices, reverse=True)
print("Sorted indices (descending):", sorted_indices)
# Remove elements using pop()
for index in sorted_indices:
if index < len(input_list):
removed_element = input_list.pop(index)
print(f"Removed element at index {index}: {removed_element}")
print("Final list:", input_list)
Original list: ['Welcome', 20, 'to', 'tutorialspoint', 30, 'python'] Indices to remove: [1, 4, 5] Sorted indices (descending): [5, 4, 1] Removed element at index 5: python Removed element at index 4: 30 Removed element at index 1: 20 Final list: ['Welcome', 'to', 'tutorialspoint']
Method 2: Using del Keyword
The del keyword removes elements at specified indices without returning them ?
Syntax
del list_name[index]
Example
# Input list
input_list = ["Welcome", 20, "to", "tutorialspoint", 30, "python"]
print("Original list:", input_list)
# Indices to remove
given_indices = [0, 3]
print("Indices to remove:", given_indices)
# Sort indices in descending order
sorted_indices = sorted(given_indices, reverse=True)
# Remove elements using del keyword
for index in sorted_indices:
if index < len(input_list):
print(f"Removing element at index {index}: {input_list[index]}")
del input_list[index]
print("Final list:", input_list)
Original list: ['Welcome', 20, 'to', 'tutorialspoint', 30, 'python'] Indices to remove: [0, 3] Removing element at index 3: tutorialspoint Removing element at index 0: Welcome Final list: [20, 'to', 30, 'python']
Comparison of Methods
| Method | Returns Removed Element? | Best For |
|---|---|---|
pop() |
Yes | When you need the removed values |
del |
No | Simple deletion without return value |
Complete Example with Error Handling
def remove_elements_by_indices(original_list, indices):
# Create a copy to avoid modifying the original
result_list = original_list.copy()
# Sort indices in descending order
sorted_indices = sorted(indices, reverse=True)
# Remove elements
for index in sorted_indices:
if 0 <= index < len(result_list):
del result_list[index]
else:
print(f"Warning: Index {index} is out of range")
return result_list
# Test the function
data = ["apple", "banana", "cherry", "date", "elderberry"]
indices_to_remove = [1, 3, 10] # 10 is out of range
print("Original:", data)
print("Indices to remove:", indices_to_remove)
result = remove_elements_by_indices(data, indices_to_remove)
print("Result:", result)
Original: ['apple', 'banana', 'cherry', 'date', 'elderberry'] Indices to remove: [1, 3, 10] Warning: Index 10 is out of range Result: ['apple', 'cherry', 'elderberry']
Conclusion
Always sort indices in descending order when removing multiple elements to avoid index shifting issues. Use pop() when you need the removed values, and del for simple deletion operations.
