Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python extend output [[...]] [duplicate]

My lecturer has set several questions on python, and this one has got me confused, I dont understand what is happening.

x = [[]]
x[0].extend(x)

Python tells me, after running this that x is [[...]], what does the ... mean?

I get even more confused when the result of the following is just [[]]

y = [] # equivalent to x[0]
x = [[]]
y.extend(x)

If y is calculated to be [[]] shouldn't x be calculated to simply being [[[]]]?

What is extend doing? and what does the ... mean?

like image 588
CGA1123 Avatar asked Nov 29 '15 18:11

CGA1123


1 Answers

The ... indicates that the list contains a recursive loop, i.e., at some level something contains itself. This is because you extended x with x, so you essentially put x inside itself.

There is no ... in the second example because y is a distinct object. Although it happens to be equal to x[0] in that both are empty lists, they are not the same empty list.

like image 62
BrenBarn Avatar answered Nov 11 '22 19:11

BrenBarn