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
Python - Replace sublist with others in list
Replacing a sublist (portion of a list) with another list is a common operation in Python. This article explores multiple approaches to replace elements at specific index ranges with new values.
Let's understand the problem with an example ?
original_list = [1, 2, 3, 4, 5, 6] new_items = [7, 8, 9] # Replace elements from index 2 to 5 (exclusive) with new_items # Result should be: [1, 2, 7, 8, 9, 6]
Using List Slicing (Recommended)
The simplest and most Pythonic approach uses slice assignment to replace a sublist ?
def replace_sublist(original_list, start, end, new_items):
original_list[start:end] = new_items
return original_list
original_list = [1, 2, 3, 4, 5, 6]
new_items = [7, 8, 9]
result = replace_sublist(original_list.copy(), 2, 5, new_items)
print("Original list:", [1, 2, 3, 4, 5, 6])
print("New items:", new_items)
print("Result:", result)
Original list: [1, 2, 3, 4, 5, 6] New items: [7, 8, 9] Result: [1, 2, 7, 8, 9, 6]
Using List Concatenation
Create a new list by concatenating three parts: before, replacement, and after ?
def replace_sublist(original_list, start, end, new_items):
return original_list[:start] + new_items + original_list[end:]
original_list = [1, 2, 3, 4, 5, 6]
new_items = [7, 8, 9]
result = replace_sublist(original_list, 2, 5, new_items)
print("Original list:", original_list)
print("New items:", new_items)
print("Result:", result)
Original list: [1, 2, 3, 4, 5, 6] New items: [7, 8, 9] Result: [1, 2, 7, 8, 9, 6]
Using List Comprehension
Build a new list conditionally replacing elements based on their index position ?
def replace_sublist(original_list, start, end, new_items):
return [new_items[i - start] if start <= i < end and i - start < len(new_items)
else element for i, element in enumerate(original_list)]
original_list = [1, 2, 3, 4, 5, 6]
new_items = [7, 8, 9]
result = replace_sublist(original_list, 2, 5, new_items)
print("Original list:", original_list)
print("New items:", new_items)
print("Result:", result)
Original list: [1, 2, 3, 4, 5, 6] New items: [7, 8, 9] Result: [1, 2, 7, 8, 9, 6]
Using Index-Based Replacement
Replace elements one by one using a loop and direct indexing ?
def replace_sublist(original_list, start, end, new_items):
result = original_list.copy()
for i in range(start, min(end, start + len(new_items))):
if i - start < len(new_items):
result[i] = new_items[i - start]
return result
original_list = [1, 2, 3, 4, 5, 6]
new_items = [7, 8, 9]
result = replace_sublist(original_list, 2, 5, new_items)
print("Original list:", original_list)
print("New items:", new_items)
print("Result:", result)
Original list: [1, 2, 3, 4, 5, 6] New items: [7, 8, 9] Result: [1, 2, 7, 8, 9, 6]
Comparison
| Method | Modifies Original? | Performance | Readability |
|---|---|---|---|
| List Slicing | Yes | Fast | Excellent |
| Concatenation | No | Good | Very Good |
| List Comprehension | No | Good | Complex |
| Index-Based | No | Slower | Good |
Conclusion
List slicing (`list[start:end] = new_items`) is the most efficient and readable method for replacing sublists. Use concatenation when you need to preserve the original list unchanged.
