1

I have a small graph enter image description here.

I want to add nodes to a 2D list like this. enter image description here

0 is connected to nodes 1 and 2, so at index 0 these two nodes are inserted and so on. The program should take input from user. For simplification, I have written this code.

l = [[]]*4

l[0].append(1)
l[0].append(2)

l[1].append(0)
l[1].append(3)

l[2].append(0)
l[2].append(3)

l[3].append(1)
l[3].append(2)

print(l, end="\n")

But the output is [[1, 2, 0, 3, 0, 3, 1, 2], [1, 2, 0, 3, 0, 3, 1, 2], [1, 2, 0, 3, 0, 3, 1, 2], [1, 2, 0, 3, 0, 3, 1, 2]]

And I want output as in the second image. How to achieve that?

coder
  • 143
  • 10

0 Answers0