Specific iterator in one line

Chris Rebert clp2 at rebertia.com
Tue Jun 30 11:31:09 CEST 2009


On Tue, Jun 30, 2009 at 2:19 AM, Bruno
Desthuilliers<bruno.42.desthuilliers at websiteburo.invalid> wrote:
> 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 ?-)

http://en.wikipedia.org/wiki/Code_golf
It's the sport the OP is playing.

Cheers,
Chris



More information about the Python-list mailing list