I tried with a topic of lists | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I tried with a topic of lists

num=[7,7,7,7,7] num[2]=num print(num) ....as ouput I got [7,7,[...],7,7] ..can anyone plz explain reason for [...] This

22nd Feb 2020, 8:11 AM
Ana
5 Answers
+ 2
Priya You're welcome.
22nd Feb 2020, 8:30 AM
Mihai Apostol
Mihai Apostol - avatar
+ 3
You created a self-referencing list... Edit. Try to print num[2], num[2][2], num[2][2][2]....
22nd Feb 2020, 8:17 AM
Mihai Apostol
Mihai Apostol - avatar
+ 3
Priya It isn't giving output as [7,7,[7,7,7,7,7],7,7] because everytime the element at index 2 is the list itself. it would look something like [7,7,[7,7,[7,7,[7,7,[7,7,[.......],7,7],7,7],7,7],7,7],7,7] if you want to see it with variable name it would be num = [7,7,num,7,7] where num is again [7,7,num,7,7] and this continues. You can say it is infinite. It will be a self referencing list as Mihai Apostol said.
22nd Feb 2020, 8:26 AM
Utkarsh Sharma
Utkarsh Sharma - avatar
+ 2
Why it isn't giving output as [7,7,[7,7,7,7,7],7,7] ?????????
22nd Feb 2020, 8:20 AM
Ana
+ 2
Thanks a lot both of u to clear my doubt
22nd Feb 2020, 8:28 AM
Ana