Yesterday during a code review, I learned that once a list
has elements you can write:
fruits = []
fruits.append("Apple")
fruits.append("Orange")
fruits.append("Grapes")
if fruits:
# do something with list
It's a simple way to determine if the list
is empty or not.
Questions:
Is this behavior documented in the Python Docs? I've searched for myself, but unable to find any explanation.
At first glance it might seem
fruits
is aboolean
in theif
statement. What is the internal explanation? Is it testing theif len(fruits) > 0
? In other words, what exactly is going on internally that allows someone to writeif fruits:
despite the factfruits
is alist
?