Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python garbage collection about list append itself [duplicate]

Tags:

python

a = [2]
a.append(a)

And I print a,

[2, [...]]

Also I print a[1][0]

2

What is [...] ? and when I print a[1][0], print 2, not [...] ?

like image 561
li tain Avatar asked Jan 13 '17 09:01

li tain


2 Answers

... is the ellipsis object. Here it is issued because else it would print forever! (due to infinite recursion)

and: print(a[1][0]):

a[1] is a so a[1][0] is 2, like a[1][1][1][1][0] is 2 too.

like image 71
Jean-François Fabre Avatar answered Oct 19 '22 01:10

Jean-François Fabre


Ellipsis ... is used for slicing multidimensional numpy arrays.

The ellipsis syntax may be used to indicate selecting in full any remaining unspecified dimensions.

a[1] is [2, [...]]

so, a[1][0] is 2.

like image 44
Saurav Sahu Avatar answered Oct 19 '22 03:10

Saurav Sahu