def lines(file): # a text file
for line in file:yield line
yield "\n"
def blocks(file):
block=[]
for line in lines(file):
if not line.isspace():
block.append(line)
elif block: #a list in elif test expression
yield ''.join(block).strip()

- 9,280
- 9
- 43
- 57

- 101
- 6
-
1Yes, there can. The code you've shared demonstrates it. – pault Oct 01 '18 at 19:46
-
1Yes you can. Non-empty `list` objects evaluate to Truthy. Empty `list` objects will evaluate to Falsey. – mypetlion Oct 01 '18 at 19:47
-
1https://docs.python.org/3/library/stdtypes.html#truth-value-testing – Oct 01 '18 at 19:48
-
anything that can be passed to `bool` – juanpa.arrivillaga Oct 01 '18 at 19:48
3 Answers
So long as the list isn't empty then yes you can use a list to be evaluated as a True
bool in and if/elif/else statement. Empty lists will be False
, lists with at least 1 item will be True
You can always just try your own code out to see for yourself!

- 1,201
- 11
- 28
-
1
-
-
See the section "Answer Well-Asked Questions" in [How to Answer](https://stackoverflow.com/help/how-to-answer), particularly the bullet point regarding questions which *"...have already been asked and answered many times before"*. – Charles Duffy Oct 01 '18 at 19:51
-
1
-
@juanpa.arrivillaga I've interpreted OP's question as asking if the the object will be evaluated as `True` but technically an empty list is a valid argument too, yes. Just it'll be false. It depends how you interpret the questions I guess. – Capn Jack Oct 01 '18 at 19:53
-
@CharlesDuffy Yup, but it was a simple answer so I figured it'd be easier for OP to just get the answer here as well. If it was a more involved answer I'd have left it. I'm also not the only user that answered it (see below). – Capn Jack Oct 01 '18 at 19:55
-
*nod* -- the others should know better too. (BTW, an option to distinguish rep farming from trying to provide a useful summary to a bad answer is use of the "Community Wiki" option when posting an answer to something you know/believe isn't a valid question). – Charles Duffy Oct 01 '18 at 20:19
-
@CharlesDuffy I'm not familiar with the Community Wiki option, mind explaining a bit more? – Capn Jack Oct 01 '18 at 20:43
-
Selecting Community Wiki "disowns" a post -- grants permission for others to edit it at-will, showing ownership as a percentage of content contributed rather than a single name, and disclaims any rep gain from its upvotes. See https://stackoverflow.com/questions/52407041/packages-do-not-match-the-hashes-error-with-pip/52407348#52407348 for an example of a CW post with my name on it. – Charles Duffy Oct 01 '18 at 22:51
Yes, it is valid. Empty lists yield False
, otherwise the expression yields True
.
If you want more clarity, you can also use a more verbose form:
if ...
...
elif len(block) > 0:
...

- 16,086
- 4
- 44
- 68
Yes, because any object can be tested for truth value in Python as stated here:
Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below.
By default, an object is considered true unless its class defines either a bool() method that returns False or a len() method that returns zero, when called with the object. [1] Here are most of the built-in objects considered false:
- constants defined to be false: None and False.
- zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
- empty sequences and collections: '', (), [], {}, set(), range(0)
Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

- 1
- 1

- 6,316
- 1
- 18
- 40