Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printed a list, three dots appeared inside sublists

Tags:

python

list

I printed out the contents of a list, and i got the following output:

[[...], [...], [...], [...], [...], [...]]

What are these strange dots?

I used python 2.7.3

like image 217
Kompi Avatar asked Dec 13 '12 01:12

Kompi


People also ask

What is the difference between a list and a sub list?

The difference between a sentence and a list is that the elements of a sentence must be words, whereas the elements of a list can be anything at all: words, #t , procedures, or other lists. (A list that's an element of another list is called a sublist.

What are lists used for in Python?

List. Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.


1 Answers

Probably you accidentally built a list containing a reference to itself (or here, lots of references):

>>> a = ['x']
>>> a
['x']
>>> a[0] = a
>>> a
[[...]]

The three dots are used so that the string representation doesn't drown in recursion. You can verify this by using id and the is operator:

>>> id(a)
165875500
>>> id(a[0])
165875500
>>> a is a[0]
True
like image 171
DSM Avatar answered Sep 28 '22 20:09

DSM