0%

initilize a 2-D empty list

In python, we sometimes need to define an empty 2-D list to store something, i.e., tensors for different sizes at different grid point in the 2-D list.

Using Google, you may initialize the list in this way:

1
a = [0*dim_0]*dim_1

In this way, the reference of the second dim is the same, when you change a[0][1], a[0][0] will also be changed. (if a stores tensors)

The correct way is:

1
2
3
4
for i in range(dim_0):
a.append([])
for j in range(dim_1):
a[i].append([])