+ 2

What does mean this Python sintax?

a = [<something>] a[:] = [<new something>]#why ([:]) used here ?

7th Jun 2020, 5:28 PM
Alexandr Goncharenko
Alexandr Goncharenko - avatar
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.
7th Jun 2020, 5:54 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
7th Jun 2020, 5:36 PM
Avinesh
Avinesh - avatar
+ 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
7th Jun 2020, 5:42 PM
Alexandr Goncharenko
Alexandr Goncharenko - avatar
+ 1
Problem is what code works there with one variable (a). I understood why in second line used ([:])
7th Jun 2020, 5:59 PM
Alexandr Goncharenko
Alexandr Goncharenko - avatar
+ 1
I don't have problem. I just saw example in one code and I understood mean of this syntax
7th Jun 2020, 6:06 PM
Alexandr Goncharenko
Alexandr Goncharenko - avatar
+ 1
Ok perfect. Good luck...!!!
7th Jun 2020, 6:07 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
Alexandr Goncharenko Not getting. Could you please explain your problem?
7th Jun 2020, 6:04 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar