Try deepcopy :
from copy import deepcopy
a = [[] for e in range(6)]
b=deepcopy(a)
c=deepcopy(a)
Why deep copy why not copy:
Here is reason:
if you do with simple copy then if you modify original it will modify the copied nested list because its shallow copy
from copy import deepcopy
a = [[] for e in range(6)]
b=a[:]
c=a[:]
for i in a:
i.append(22)
print(a)
print(b,c)
output:
[[22], [22], [22], [22], [22], [22]]
[[22], [22], [22], [22], [22], [22]] [[22], [22], [22], [22], [22], [22]]
But with deepcopy :
from copy import deepcopy
a = [[] for e in range(6)]
b=deepcopy(a)
c=deepcopy(a)
for i in a:
i.append(22)
print(a)
print(b,c)
output:
[[22], [22], [22], [22], [22], [22]]
[[], [], [], [], [], []] [[], [], [], [], [], []]
a = b = c = [[] for e in range(6)]you would leta,bandcrefer to the same list.