comprehensions was Re: Switch statements again

Jack Diederich jack at performancedrivers.com
Thu Jan 16 12:54:38 EST 2003


On Thu, Jan 16, 2003 at 09:39:02AM -0800, Dave Brueck wrote:
> On Thu, 16 Jan 2003, Sean 'Shaleh' Perry wrote:
> 
> > 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.
> [snip example]
> 
> > > 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.
[snip]
> 
> [x for x in S if x > 5]
> 
> always makes far more sense to me than:
> 
> filter(lambda x: x>5, S)
> 
> for various reasons, including the fact "for x in S", "if x > 5", and
> "[...stuff...]" all mean something to me already whereas filter(someArg1,
> someArg2) does not, and lambda requires me to think more. :)
> 
> FWIW, I found simple list comprehensions to be one of the least taxing
> Pythonisms to get used to - IMO the syntax maps very obviously to the
> behavior.

TMTOWTDI aside, I find filter and map much easier to pick out when reading
through code.

l = map(modify_func, l)
       ^
# I can stop reading at ^, we're modifying the items in the list

l = filter(test_func, l)
          ^
# I can stop reading at ^, we're reducing the size of the list

l = [x for x in l if x > 5]
                  ^^^^^^^^^

The actual action is hidden at the end, we're doing a filter()

Less of a good reason, but map() and filter() exist in other languages
so they are more broadly recognizable even to newbies.
               
-jackdied





More information about the Python-list mailing list