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 How to copy a nested list
In this tutorial, we are going to see different ways to copy a nested list in Python. A nested list is a list that contains other lists as elements. When copying nested lists, we need to create independent copies of both the outer list and inner lists to avoid unwanted modifications.
Let's explore three effective methods to achieve this ?
Using Nested Loops
The most straightforward approach uses nested loops to manually copy each element ?
# initializing a list
nested_list = [[1, 2], [3, 4], [5, 6, 7]]
# empty list
copy_list = []
for sub_list in nested_list:
# temporary list
temp = []
# iterating over the sub_list
for element in sub_list:
# appending the element to temp list
temp.append(element)
# appending the temp list to copy
copy_list.append(temp)
# printing the list
print(copy_list)
# verify independence
nested_list[0][0] = 999
print("Original after modification:", nested_list)
print("Copy remains unchanged:", copy_list)
[[1, 2], [3, 4], [5, 6, 7]] Original after modification: [[999, 2], [3, 4], [5, 6, 7]] Copy remains unchanged: [[1, 2], [3, 4], [5, 6, 7]]
Using List Comprehension with Unpacking
A more concise approach uses list comprehension with the unpacking operator * ?
# initializing a list
nested_list = [[1, 2], [3, 4], [5, 6, 7]]
# copying using list comprehension
copy_list = [[*sub_list] for sub_list in nested_list]
# printing the copy
print(copy_list)
# verify independence
nested_list[1].append(8)
print("Original after modification:", nested_list)
print("Copy remains unchanged:", copy_list)
[[1, 2], [3, 4], [5, 6, 7]] Original after modification: [[1, 2], [3, 4, 8], [5, 6, 7]] Copy remains unchanged: [[1, 2], [3, 4], [5, 6, 7]]
Using copy.deepcopy()
The copy.deepcopy() method provides the most robust solution for deeply nested structures ?
# importing the copy module
import copy
# initializing a list
nested_list = [[1, 2], [3, 4], [5, 6, 7]]
# copying using deepcopy
copy_list = copy.deepcopy(nested_list)
# printing the copy
print(copy_list)
# verify independence with deeply nested modification
nested_list[2] = [99, 100]
print("Original after modification:", nested_list)
print("Copy remains unchanged:", copy_list)
[[1, 2], [3, 4], [5, 6, 7]] Original after modification: [[1, 2], [3, 4], [99, 100]] Copy remains unchanged: [[1, 2], [3, 4], [5, 6, 7]]
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| Nested Loops | Verbose | Good | Learning/Understanding |
| List Comprehension | Concise | Fastest | Simple nested lists |
| deepcopy() | Simple | Slowest | Complex nested structures |
Conclusion
For simple nested lists, use list comprehension with unpacking for optimal performance. Use copy.deepcopy() when dealing with complex nested structures or when you need guaranteed independence of all nested levels.
