Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Append a list to the same list

Tags:

python-2.7

I am having a very basic doubt. Consider the following example:

Case 1:

a=[1,2,3]
b=[4,5,6]
a.append(b) #print a will give [1,2,3,4,5,6]

Case 2:

a=[1,2,3]
a.append(a) # print a gives [1,2,3,...]

I understand the .append in python appends the values of the variable to the end of the variable it's appended to. However, i don't understand the behavior of the '...' in the Case 2.

like image 652
SeasonalShot Avatar asked Sep 06 '15 02:09

SeasonalShot


1 Answers

The dots indicate that a list contains a reference to itself. It just avoids an infinite recursion during the print.

like image 155
HelloWorld Avatar answered Sep 22 '22 10:09

HelloWorld