python2.4 generator expression > python2.3 list expression

Steven Bethard steven.bethard at gmail.com
Mon Feb 21 00:03:58 EST 2005


snacktime wrote:
> I need to convert a generator expression to a list expression so it
> will work under python 2.3.
> 
> I rewrote this:
> 
> for c in range(128):
>   even_odd = (sum(bool(c & 1<<b) for b in range(8))) & 1
> 
> As this:
> 
> for c in range(128):
>   bo = [bool(c & 1<<b) for b in range(8)]
>   even_odd = sum(bo) & 1
> 
> Seems to work, is there a better way to do this?

Well, if you were happy with your generator expression, you can use 
almost exactly the same syntax:

for c in range(128):
   even_odd = (sum([bool(c & 1<<b) for b in range(8)])) & 1

No need for the 'bo' variable...

STeVe



More information about the Python-list mailing list