Removing dictionary-keys not in a set?

Steven Bethard steven.bethard at gmail.com
Mon Apr 18 13:53:36 EDT 2005


Tim N. van der Leeuw wrote:
> In my taste, using generator expressions with conditions actually
> doesn't look very readable; it hides the set-membership test under the
> syntactic clutter.

You might try different indentation.  I find that I write a lot of my 
list comprehensions and generator expressions like:

dict((k, v)
      for k, v in d.iteritems()
      if k in s)

It looks much more like the for-loop in my mind, and I can easily spot 
each of the for- and if-expressions in the genexp.

STeVe



More information about the Python-list mailing list