0

List b’s value also changing for each iteration..why?

https://code.sololearn.com/cA0A15a3A21a/?ref=app

16th Jan 2021, 8:41 PM
Muhammed Niyas
Muhammed Niyas - avatar
3 odpowiedzi
0
Muhammed Niyas, maybe you should append a copy of l on line 36: if z == "print": b.append(l.copy())
16th Jan 2021, 10:45 PM
John Robotane
John Robotane - avatar
0
but why I cant append l? John Robotane
17th Jan 2021, 7:09 AM
Muhammed Niyas
Muhammed Niyas - avatar
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
17th Jan 2021, 7:21 AM
Angelo
Angelo - avatar