I want to make a list of lists in python. I am initializing this list like:
n = 3
adj = [[]]*n
So now when I print adj
, I get:
[[], [], []]
...which is fine.
Now, I want to append values to a particular list (one particular index). I have done this:
adj[0].append(1)
Hence, my expected output is:
[[1], [], []]
However the output is:
[[1], [1], [1]]
I can't seem to understand why this is so. Probably I am not understanding something about how Python's lists work, or am making an utterly silly mistake. Any help would be greatly appreciated.
Additionally, I would really appreciate a suggestion as to how I can get my desired output.