How to clone a two-dimensional list in Python

2 Answers

0 votes
import copy

list2D = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]

cloned_list = copy.deepcopy(list2D)

print(cloned_list)


  
'''
run:
  
[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
  
'''

 



answered Mar 7, 2025 by avibootz
0 votes
list2D = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]

cloned_list = [row[:] for row in list2D]

print(cloned_list)


  
'''
run:
  
[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
  
'''

 



answered Mar 7, 2025 by avibootz

Related questions

1 answer 86 views
1 answer 94 views
3 answers 124 views
1 answer 96 views
1 answer 156 views
1 answer 103 views
1 answer 110 views
110 views asked Mar 8, 2025 by avibootz
...