comprehensions was Re: Switch statements again

Sean 'Shaleh' Perry shalehperry at attbi.com
Thu Jan 16 10:36:18 EST 2003


On Thursday 16 January 2003 07:24, A. Lloyd Flanagan wrote:
> Jack Diederich <jack at performancedrivers.com> wrote in message
> news:<mailman.1042672181.22990.python-list at python.org>...
>
> > using a list comprehension or zip().  Comprehensions always
> > hurt readability (ALWAYS), and we don't need the speedup
> > here so we do it explicitly.
>
> Personally, I find list comprehensions to be both very readable and
> compact.  Maybe it's a matter of how familiar you are with them.
>
> x = [a[3].someMethod() for a in some_list if a[2] == y]
>
> seems clearer than:
>
> x = []
> for a in some_list:
>     if a[2] == y:
>         x += a[3].someMethod()
>
> after all, a list comprehension is less general than a for-loop, so
> you know more about what it does before you read it.

comprehensions are hard on the reader until they start using them.  I am in 
the archives of this and -tutor stating my disklike for them for this reason.  
Now that I routinely use them in my own code I do like them but there is a 
significant penalty to the new Pythoner.

I do like being able to use map/zip like functionality without lambdas.  
However I still use reduce() because I do not want to build a list just to 
find out the value I am really looking for.  I often use a 'for loop' if I 
need to run a function on every item in a list but I do not need a new list.





More information about the Python-list mailing list