checking if a list is empty

Terry Reedy tjreedy at udel.edu
Sun May 8 18:41:20 EDT 2011


On 5/8/2011 10:07 AM, Steven D'Aprano wrote:

> Because the test of "is this nothing, or something?" is a common, useful
> test:

Because inductive algorithms commonly branch on 'input is something' 
(not done, change args toward 'nothing'and recurse or iterate) versus 
'input is nothing (done, return base expression).

> 0 (nothing) vs 1, 2, 3, ... (something)
> empty list versus non-empty list
> empty dict versus non-empty dict
> empty set versus non-empty set
> empty string versus non-empty string

and non-inductive algorithms also branch on the same question.

> no search results returned versus some search results returned
> no network connection available versus some network connection available
> no food in the cupboard versus food in the cupboard
>
> This is a fundamental distinction which is very useful in practice.

Definitely. Python commonly hides the distinction inside of iterators 
called by for statememts, but StopIteration is understood as 'collection 
is empty'. An I suspect something vs. nothing is also the most common 
overt condition in if and while statements.

-- 
Terry Jan Reedy




More information about the Python-list mailing list