What is the difference between a=list and a=list[:] | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

What is the difference between a=list and a=list[:]

Both of them are giving me a different output..in this case... U can try by removing or adding [:] https://code.sololearn.com/cT6ryN1RrlxK/?ref=app

7th Oct 2019, 11:19 AM
Thashreef Mohammed
Thashreef Mohammed - avatar
4 Respostas
+ 4
a = [1, 2, 3] b = a c = a[:] b is the same list as a, which means you just gave it a different name, if you change elements in b, they will be changed in a too, as they are one single list with two different names. c is just a part of a, and in this case the whole list, but it is independent, operations that you make on c won't affect a.
7th Oct 2019, 11:34 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
Hahah once I spent an hour trying to figure out what was wrong with my code, untill I realized it! Happy to help!
7th Oct 2019, 11:39 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Aymane Boukrouh That was a new knowledge for me! I never know changing the values of a effect b since b is a different variable.
7th Oct 2019, 11:38 AM
Thashreef Mohammed
Thashreef Mohammed - avatar
+ 1
Thnx! And doubt solved
7th Oct 2019, 11:38 AM
Thashreef Mohammed
Thashreef Mohammed - avatar