What is the output of the following code? #PYTHON
What is the output of the following code? L,D=[],[] for x in range(4): D.append(x) L.append(D) print(L[0]) What I think the answer should be... 1st if all L and D are both empty lists. Then we in the next line we say for x in range(4) so the range will be [0,1,2,3] . Now, with the value x=0 the value 0 should be added to the list and now the D is [0]. And similarly in next step we add D to List L which should make L=[[0]] So when the for loop runs 2nd time with x=1 D becomes [0,1] And L becomes [[0],[0,1]] And like this when the for loop runs for the last time with x= 3 D will become [0,1,2,3] And L will become [[0],[0,1],[0,1,2],[0,1,2,3]] So at last when we give command print(L[0]) Shouldn't it print [0]???? But the answer was [0,1,2,3] I think this would be the answer if the code looks like this L,D=[],[] for x in range(4): D.append(x) L.append(D) print(L[0]) In this L.append(D) is out of for loop so when the for loop is done with D=[0,1,2,3].. then L.append(D) will take the value[ [0,1,2,3]]