Python | How to copy a nested list
Last Updated :
05 Dec, 2023
Copying a nested list in Python involves preserving both the structure and values of the original. One common method is using copy.deepcopy()
from the copy
module. Additionally, list comprehensions with slicing offer a concise approach for creating an independent duplicate. In the previous article, we have seen how to clone or Copy a list, now let’s see how to copy a nested list in Python.
Copy a Nested List in Python
Below are the ways by which we can copy a nested list in Python:
- Using Iteration
- Using deepcopy
- Using list comprehension and slicing
- Using the json module
Copy a Nested List Using Iteration
In this example, the code iterates through a nested list (Input_list
) using nested loops to create a new list (Output
). The inner loop assigns values to a temporary list, which is then appended to the outer list, resulting in a copy of the structure of the original nested list.
Python3
Input_list = [[ 0 , 1 , 2 ], [ 3 , 4 , 5 ]]
Output = []
for x in range ( len (Input_list)):
temp = []
for elem in Input_list[x]:
temp.append(elem)
Output.append(temp)
print ( "Initial list is:" )
print (Input_list)
print ( "New assigned list is" )
print (Output)
|
Output
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
Time Complexity: O(n), where n is the number of elements in the list.
Space Complexity: O(n)
Python Copy a Nested List Using Deepcopy
In this example, a nested list Input
is duplicated to Output
using copy.deepcopy()
to ensure an independent copy of both the outer and inner list elements. The program then prints the initial and newly assigned lists for verification.
Python3
import copy
Input = [[ 1 , 0 , 1 ], [ 1 , 0 , 1 ]]
Output = copy.deepcopy( Input )
print ( "Initial list is:" )
print ( Input )
print ( "New assigned list is" )
print (Output)
|
Output
Initial list is:
[[1, 0, 1], [1, 0, 1]]
New assigned list is
[[1, 0, 1], [1, 0, 1]]
Time Complexity: O(n), where n is the number of elements in the list.
Space Complexity: O(n)
Copy a Nested List Using List Comprehension and Slicing
In this example, a nested list Input_list
is efficiently duplicated to out_list
using a list comprehension and slicing. This method creates a new list with the same structure and values, as demonstrated by printing both the initial and newly assigned lists.
Python3
Input_list = [[ 0 , 1 , 2 ], [ 3 , 4 , 5 ], [ 0 , 1 , 8 ]]
out_list = [ele[:] for ele in Input_list]
print ( "Initial list is:" )
print (Input_list)
print ( "New assigned list is" )
print (out_list)
|
Output
Initial list is:
[[0, 1, 2], [3, 4, 5], [0, 1, 8]]
New assigned list is
[[0, 1, 2], [3, 4, 5], [0, 1, 8]]
Time Complexity: O(n), where n is the number of elements in the list.
Space Complexity: O(n)
How to Copy a Nested List Using the Json Module
The json module can be used to serialize and deserialize data in JSON format. This means that you can convert the nested list to a JSON string, and then parse the string to create a new list. This method can be useful if the elements in the nested list are serializable, but may not be suitable if the elements are not serializable.
Python3
import json
Input_list = [[ 0 , 1 , 2 ], [ 3 , 4 , 5 ]]
json_str = json.dumps(Input_list)
Output = json.loads(json_str)
print ( "Initial list is:" )
print (Input_list)
print ( "New assigned list is" )
print (Output)
|
Output
Initial list is:
[[0, 1, 2], [3, 4, 5]]
New assigned list is
[[0, 1, 2], [3, 4, 5]]
Time Complexity: O(n), where n is the number of elements in the list.
Space Complexity: O(n)
Please Login to comment...