Specific iterator in one line

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Tue Jun 30 11:19:54 CEST 2009


Chris Rebert a écrit :
(snip)
>>>> reduce(lambda x,y:x+y,({1:['b']*2,0:['a']}[z] for z in [1, 0, 0, 1]))
> ['b', 'b', 'a', 'a', 'b', 'b']
> 
> 69 chars long (plus or minus how the input list is written).

You can save 4 more characters using tumple dispatch instead of dict 
dispatch:

reduce(lambda x,y : x+y, ((['a'], ['b']*2)[z] for z in [1, 0, 0, 1]))

> Where's my golf trophy? ;)

golf ? Why golf ?-)



More information about the Python-list mailing list