0

Please explain this code to me in Python

D = [] L = [] for i in range(4): D.append(i) L.append(D) print(L) Why the result [[0,1,2,3] ,[0,1,2,3], [0,1,2,3], [0,1,2,3]]? Thank you. All.

11th Aug 2023, 3:10 AM
Oliver Pasaribu
18 Respuestas
+ 5
In your code, all lines of L are updated when moving to the next i, since D changes. It happens like this: i = 0 L = [[0]] i = 1 L = [[0, 1], [0, 1]] and so on. If you want to make a sequential addition (also note that in your example the variable D is not needed, you can get rid of it): https://code.sololearn.com/cdz5OxB44j6f/?ref=app
11th Aug 2023, 5:27 AM
Jane
Jane - avatar
+ 3
This is how Python works. By updating D, the variable D will be updated in L. D = [] L = [] for i = 0: D = [0] L = [[D]] = [[0]] for i = 1: D = [0, 1] L = [[D][D]] = [[0, 1][0, 1]] for i = 2: D = [0, 1, 2] L = [[D][D][D]] = [[0, 1, 2][0, 1, 2][0, 1, 2]] for i = 3: D = [0, 1, 2, 3] L = [[D][D][D][D]] = [[0, 1, 2, 3][0, 1, 2, 3][0, 1, 2, 3][0, 1, 2, 3]]
11th Aug 2023, 7:57 AM
Jane
Jane - avatar
+ 1
In Python, everything is always passed by reference. The assignment operation does not copy the object, it only creates a reference to the object. Therefore, for example, here b will change if we change a: https://code.sololearn.com/cQTaPMLic37r/?ref=app In your case, the same thing happens. You are not adding D, but a reference to D, so there is a change in the entire list of L. L stores four references to the same object. To solve this problem, you can also use the copy module: https://code.sololearn.com/c1AcvOsWjPS5/?ref=app
11th Aug 2023, 10:15 AM
Jane
Jane - avatar
+ 1
As simple as this Since D=[i] and L=[D] Then when i=0 D=[[i]] which is (0) L=[[D]] which is ([0]) So If I=1 D=[0,1] L=[[0,1]] If I=2 D=[0,1,2] L=[[0,1,2][0,1,2]] This will keep going until it gets to the range of (i) which is (4) Note: for every range of i, the places of(D) in (L) increases Was this helpful?
12th Aug 2023, 6:17 PM
Emmy
Emmy - avatar
+ 1
created two empty lists to append, then a for loop on the range function for 4=0123.Append=adds items to the last. Therefore appending 0123 in D then this D list appended in L for 0123 times. thus python decodes line by line. hope it helps
13th Aug 2023, 1:14 AM
Mohammed Faizuddin
Mohammed Faizuddin - avatar
0
The order of the for loop in this code works like: ` D.append(i) #Append i to D 4 times, where each time i is updated because of the for loop (This will keep executing until the condition is no longer met) => D = [0,1,2,3]. [*After done executing*] || * V L.append(D) #Append D to L 4 times (This will keep executing until the condition is no longer met) => L = [[0,1,2,3],[0,1,2,3],[0,1,2,3],[0,1,2,3]]. ` Remember, the code order is from up to down.. so when a line of code is done executing, it will go to the next line of code, except for some cases, such as if-else statement..
11th Aug 2023, 3:15 AM
Dragon RB
Dragon RB - avatar
0
What about this: i = 0: D = [0] L = [[0]] i = 1 D = [0, 1] And L should be or supposed to be:: L = L.append(D) =[ [0],[0,1]] i = 2 D = [0,1,2] L = [ [0], [[0,1], [0,1,2] i= 3 D = [0, 1, 2, 3] L = [ [0], [0,1], [0,1,2], [0,1,2,3] ] Stop Iteration. What do you think ?
11th Aug 2023, 3:43 AM
Oliver Pasaribu
0
Oliver Pasaribu Yes, L is [[0],[0,1]] in this case.
11th Aug 2023, 3:46 AM
Dragon RB
Dragon RB - avatar
0
Yes, it behaves like two different sequence of for loop: D = [] L = [] for i in range(4): D.append(i) for j in range(4) L.append(D)
11th Aug 2023, 4:26 AM
Oliver Pasaribu
0
Why? You know that [0], [[0]] and [], has a different meaning of one to each other.
11th Aug 2023, 7:19 AM
Oliver Pasaribu
0
I know that [0], [[0]] and [] have different meanings for each other. But I didn't know what kind of conclusion you want to get, so I offered some options.
11th Aug 2023, 8:01 AM
Jane
Jane - avatar
0
Hi, this update invokes function append(), L.append(D) while D is [0] become [0,1], then [0,1,2] and finally [0,1,2,3]. so append it must be add new element to existing list, instead of overwrite the old content of the list.
11th Aug 2023, 9:33 AM
Oliver Pasaribu
0
Please observe this. a = [] a.append([0]) print(a) a.append([0,1]) print(a) aappend([0,1,2]) print(a) a.append([0,1,2,3]) print(a)
11th Aug 2023, 9:38 AM
Oliver Pasaribu
0
This is exactly the same to this I think: D = [] L = [] for i in range(4): #two statement nested #inside for D.append(i) L.append(D) // out from the for.. in loop: print(L)
11th Aug 2023, 9:41 AM
Oliver Pasaribu
0
D = [] L = [] for i in range(4): D.append(i) L.append(D) print(L) First you should try this then you are easily understand the working of for loops
12th Aug 2023, 8:25 AM
Yash Tripathi
0
Emmy, how could when i=1, L =[[0][ then L += D = [ [0,1] ], [[0]] then [[0]] +![0,1] should be [ [0], [0,1] ] why [ [0,1] ]? Do you think: [ [0], [0,1] ] is identical to [ [0,1]]. Just different writing style: [ [] ] , [ [ [] ] ]. to [ [] ] , [ [], [] ] ?
12th Aug 2023, 10:19 PM
Oliver Pasaribu
0
Bhai code run hote nahe
13th Aug 2023, 3:01 AM
Tanmay Khavare
Tanmay Khavare - avatar
0
I have tested, this original code: D = [] L = [] for i in range(4): D.append(i) L.append(D) give the same result as code below: D = [] L = [] for i in range(4): D.append(i) for i in range(4): L.append(D)
13th Aug 2023, 5:29 AM
Oliver Pasaribu