Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does [...] (an ellipsis) in a list mean in Python? [duplicate]

Tags:

python

I just got some weird output of a python script:

[[(7, 6), (6, 4), (7, 2)], [...], [...], [...], [(7, 6), (8, 4), (7, 2)], [...], [...], [...], [...], [...], [...], [...]]

The output should be a list of lists of tuples. But I have no idea why [...] appears.

What does [...] mean?

I don't think its an empty list, as an empty list were []. Are these perhaps duplicates?

like image 955
Martin Thoma Avatar asked Aug 28 '11 19:08

Martin Thoma


1 Answers

It is a recursive reference. Your list contains itself, or at least there is some kind of cycle.

Example:

x = []
x.insert(0, x)
# now the repr(x) is '[[...]]'.

The built-in repr for lists detects this situation and does not attempt to recurse on the sub-list (as it normally would), because that would lead to infinite recursion.

Note that ... doesn't necessarily tell you which list is referred to:

y, z = [], []
x = [y, z]
y.insert(0, z)
z.insert(0, y)
# looks the same as it would if y contained y and z contained z.

so repr is not really a complete serialization format for lists.

As to why you're getting them: we're not psychic, and can't fix the problem with your code unless we see the code.

like image 71
Karl Knechtel Avatar answered Oct 17 '22 08:10

Karl Knechtel