Anyway to clarify this code? (dictionaries)

bonono at gmail.com bonono at gmail.com
Tue Nov 22 23:39:05 EST 2005


Mike Meyer wrote:
> First, remember the warnings about premature optimization.

Which is why I said the one-liner(your first one) is clean and clear,
and bug free in one go.

>
>       use = set(another) - set(keys)
>       return dict([[k, another[k]] for k in use if another[k] >= x]
>
> Though I still think I prefer the longhand version:
>
>        out = dict()
>        for key in set(another) - set(keys):
>            if another[k] >= x:
>               out[k] = another[k]
This is definitely better than the other long hand version as the set
operation remove the potential problem of another[k] raise KeyError.




More information about the Python-list mailing list