Output question. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Output question.

Why is the output of the following code = [1,[2,5]]? a= [1,[2,3]] b= a[:] a[0] = 3 a[1][1] = 5 print(b)

1st Sep 2019, 6:15 PM
Gabriel Wiedemann
Gabriel Wiedemann - avatar
4 Answers
+ 3
b = a[:] copies all indexes to b, since a[1] is a list the refernce to that list get copied. when you now edit a[1] the change is in b too. you perhaps should use: import copy b = copy.deepcopy(a)
1st Sep 2019, 6:20 PM
Anton Böhler
Anton Böhler - avatar
+ 2
Sorry about that, that was my first question, I didn't think about that, my bad
1st Sep 2019, 6:37 PM
Gabriel Wiedemann
Gabriel Wiedemann - avatar
+ 1
Please mention the language in Relevant Tags rather than a sentence or question 'why on the following scrip,'. Proper use of tags helps improve search feature quality 👍
1st Sep 2019, 6:27 PM
Ipang
0
I understand, just reminding you in case you forget, no problem buddy 👍
1st Sep 2019, 6:40 PM
Ipang