0

Starting learning Python lists and stuck with the question

Simplifying I have a code:

A = []
B=[{'x':10},{'x':30}]
for elem in B:
       A.append(elem)     

for elem in B: ##changing B list
       elem['x']=elem['x']-1
print(A)

I've changed B list in the for cycle but last string output for A list is

[{'x': 9}, {'x': 29}]

So A list is affected too.

Could somebody explain why updating B list causes updating A list.

I expected A and B lists to be independent. Probably the answer is in append method documentation or area of elem ​​visibility but cant find the exact answer

PavelX
  • 9
  • 1
  • 3

0 Answers0