-1

I have a script that basically returns a tuple from a function:

results = some_function()

Then I'm checking to see if there are any results like so:

if results:
   do_something()

This returns True when the tuple has two empty lists. When I use debug mode my results are ([], []). Running len(results) produces a length of 2.

Interestingly, if I do the following:

results = ([])
print(len(results))

It prints 0. Why is that adding another list prints 2?

Should I be overriding the class method __len__ from the function producing the tuple?

hax0r_n_code
  • 5,761
  • 12
  • 59
  • 97
  • 3
    `([])` is _not a tuple at all_, it is an empty list written with parens around it. If you want a single-element tuple, add a comma: `([],)`. – RemcoGerlich Jun 10 '16 at 13:21
  • @free_mind That is *the* dupe canonical for such posts. See http://stackoverflow.com/questions/linked/12876177. Anyway, Closing as a dupe implies that your question is a good signpost for future readers to view the main question. – Bhargav Rao Jun 10 '16 at 14:23

1 Answers1

3

A tuple is defined by the comma, not the parentheses:

>>> results = ([],)
>>> print(len(results))
1
jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
Reblochon Masque
  • 33,202
  • 9
  • 48
  • 71