Initializing shallow 2D arrays in Python
|"Wild life: Heron In Shallow Water shot#2" by Jangra Works is licensed under CC BY 2.0|
You may have seen that there is an easy way to initiate an array or 2D array with default values by multiplying the array with number of elements.
This list is created as shallow list, which means outer array is pointing to the same inner arrays. When you change any value in the inner array, it will be visible to all outer arrays. Here is a good visual representation (ref).
In order to make a independent 2D elements, you may use the list comprehension with inner and outer array. This subtle difference can be difficult to debug sometimes; therefore, I don't use shallow copy for my new arrays most of the time.