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 173 views
1 answer 171 views
3 answers 271 views
1 answer 158 views
1 answer 246 views
1 answer 181 views
1 answer 204 views
204 views asked Mar 8, 2025 by avibootz
...