0

I just started learning Python and I encountered this problem:

a = [0, 1]
b = [a]
print(a, b) 

a.pop()
print(a, b)

Result:

[0, 1] [[0, 1]]
[0] [[0]]

Why does this happen and how do I avoid it? I want b = [0, 1]. As far as I understood, the b[0] was linked to a so I used b[a.copy()]. Thanks guys.

Akshat Zala
  • 710
  • 1
  • 8
  • 23
Henry N.
  • 15
  • 3

1 Answers1

0

welcome to Python,

if you want all elements of ato be passed to b you can dump them like this:

b = [*a] # is the same as [a[0], a[1], ...... ]

which will lead to b = [0,1]

Nano Byte
  • 106
  • 3