Open In App

Python | How to copy a nested list

Last Updated : 05 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# List initialization
Input_list = [[0, 1, 2], [3, 4, 5]]
Output = []
 
# Using iteration to assign values
for x in range(len(Input_list)):
    temp = []
    for elem in Input_list[x]:
        temp.append(elem)
    Output.append(temp)
 
# Printing Output
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




# Python program to copy a nested list
import copy
 
# List initialization
Input = [[1, 0, 1], [1, 0, 1]]
 
# using deepcopy
Output = copy.deepcopy(Input)
 
# Printing
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




# List initialization
Input_list = [[0, 1, 2], [3, 4, 5], [0, 1, 8]]
 
# comprehensive method
out_list = [ele[:] for ele in Input_list]
 
# Printing Output
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
 
# List initialization
Input_list = [[0, 1, 2], [3, 4, 5]]
 
# Convert the list to a JSON string
json_str = json.dumps(Input_list)
 
# Parse the JSON string to create a new list
Output = json.loads(json_str)
 
# Printing Output
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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads