How to make a copy of a 2D array in Python? [duplicate] How to make a copy of a 2D array in Python? [duplicate] python python

How to make a copy of a 2D array in Python? [duplicate]


Using deepcopy() or copy() is a good solution.For a simple 2D-array case

y = [row[:] for row in x]


Try this:

from copy import copy, deepcopyy = deepcopy(x)

I'm not sure, maybe copy() is sufficient.


For 2D arrays it's possible use map function:

old_array = [[2, 3], [4, 5]]# python2.*new_array = map(list, old_array)# python3.*new_array = list(map(list, old_array))