-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?

nullByteMe
  • 6,141
  • 13
  • 62
  • 99
  • 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
  • 115,751
  • 26
  • 228
  • 437
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80