0
List b’s value also changing for each iteration..why?
3 odpowiedzi
0
Muhammed Niyas, maybe you should append a copy of l on line 36:
if z == "print":
b.append(l.copy())
0
but why I cant append l? John Robotane
0
Becouse in python variables are actually references to objects
l=[] means that 'l' referes to a new void list
b=[] means same thing
b.append(l) means that the object pointed by 'l' is now also pointed by the last element of b
Tha means that every time you modify that list object trough 'l' you will see it also trough b
This will happen until the "reverse" case because
l = l[::-1] does not modify the list object itself, but creates a new object and now 'l' referes to it