Python list append doubt

l=[1,2]

l.append(3)

print(l)

l.append(l)

print(l)

l=l+[2,3,4,4]

print(l)

new_l=l[3]

print(new_l);

print(new_l[3]);

OUTPUT:

[1, 2, 3]

[1, 2, 3, […]]

[1, 2, 3, [1, 2, 3, […]], 2, 3, 4, 4]

[1, 2, 3, […]]

[1, 2, 3, […]]

I don;t understand this please explain

what is the meaning of […] ??

Hey @anandprakash1091971, here, [...] is just Python’s way of representing the fact that the list is embedded in itself.

Hope this helps.

explain this output
in my opinion it sholud be
[1, 2, 3, […], 2, 3, 4, 4]

also

this should print […] or [1,2,3,]

plz tell me in deep and explain elaborately

Ok let me explain you in detail :

When you do l.append(l) , then l points to [1,2,3,[…]] indicating that l contains itself right ?

Now when you do l=l+[2,3,4,4] , so the following operation takes place :

l = [1,2,3,[...]] + [2,3,4,4] 
print(l)

So when you print this the output comes out to be [1,2,3,[1,2,3,[…]],2,3,4,4] right ?
So here you would observe that the […] has been replaced by the whole l which points to [1,2,3,[…]].

Have you ever tried printing […] ? If you would print l[3][3] , then you would see that […] is nothing but the whole list [1,2,3,[…]].

Now coming to the other question you asked :

new_l = l[3] means new_l is also [1,2,3,[…]]. Here also […] means the whole combined list. So if you print new_l[3] it would again give you [1,2,3,[…]]. This would happen recursively. You can try printing new_l[3][3][3][3]…you would always get [1,2,3,[…]].

I would suggest you should try printing things to get a more clear picture of what is […]_
[…] is basically the whole combined list i.e. [1,2,3,[…]]

Hope this helps.
Happy Learning :slight_smile:

2 Likes

awesome Explanation i understand it now

thankyou very much

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.