(test) ? a:b

Chris Angelico rosuav at gmail.com
Sun Oct 26 05:34:42 CET 2014


On Sun, Oct 26, 2014 at 3:15 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Since the list items exist only to be counted, the actual item used makes no
> difference. You could use any value at all, or even a different value each
> time:
>
>     len([random.random() for line in lines if not line.strip()])
>
> What is your reason for the choice of True as the tally marker? It doesn't
> clarify your intent, or make it more obvious that you're counting the
> number of non-empty lines.

Personally, I'd use the original value. When you're counting
something, it's common to count that thing, rather than some stand-in.

len([line for line in lines if not line.strip()])

But that's no better than anything else. The only significance is that
it feels more like counting a filtered portion of the list.

ChrisA



More information about the Python-list mailing list