+ 2
What does mean this Python sintax?
a = [<something>] a[:] = [<new something>]#why ([:]) used here ?
7 Answers
+ 2
Alexandr Goncharenko
#case 1
l = [1, 2, 3]
print(id(l))
l[:] = [4, 5]
print(id(l))
#case 2
l1 = [1, 2, 5]
print(id(l1))
l1 = [4, 1]
print(id(l1))
Run above code. case 1 same list memory is updated with new values by using [:].
case 2 show different list has been created when l[:] is not used.
function id is used to print address associated with the variable.
hope it helps.
+ 3
Similar question.
https://www.sololearn.com/Discuss/2318582/?ref=app
+ 1
You understood my question. I saw this syntax in the alien code. I didn' t understand why programmer use this syntax, because of this I asked a question
+ 1
Problem is what code works there with one variable (a). I understood why in second line used ([:])
+ 1
I don't have problem. I just saw example in one code and I understood mean of this syntax
+ 1
Ok perfect. Good luck...!!!
0
Alexandr Goncharenko Not getting. Could you please explain your problem?