+ 3

python list initialized

When the LIST is initialized in different way, after assigned a new value, they have different results. I don't know why. Can you help me? Thanks. https://code.sololearn.com/c4S6j40UKagu/?ref=app

24th Aug 2018, 5:17 AM
魏琳琳
2 odpowiedzi
+ 4
This is because in list_a you copy the same references of the sublist. And in list_b you initialize sublists separetely. That is why when you assign a value to list_a sublist, all of them take this value by reference. add this to your code to see the difference: for _ in list_a: print(id(_)) for _ in list_b: print(id(_))
24th Aug 2018, 6:15 AM
strawdog
strawdog - avatar
+ 1
Because the 3 sub-lists in list_a are referencing the same list object
24th Aug 2018, 6:16 AM
hmysjiang🍀
hmysjiang🍀 - avatar