Specific iterator in one line

Tim Chase python.list at tim.thechases.com
Tue Jun 30 06:29:58 EDT 2009


>> list("".join([("a","b"*2)[x] for x in [1,0,0,1]])
>>
>> 50 characters. Do I win £5?
> 
> list("".join([("a","bb")[x] for x in [1,0,0,1]])
> 
> Or 49 :o)

Well, you have a missing ")" character, but that would be the 
49th.  You can[*] abuse python's parsing by removing certain 
spaces with

   list(''.join([('a','bb')[x]for x in[1,0,0,1]]))

bringing you down to 47.  In more recent versions of Python, you 
can then pass a generator instead of a list-comprehension:

   list(''.join(('a','bb')[x]for x in[1,0,0,1]))

bringing you to 45.

-tkc

[*] not *should*, but *can*







More information about the Python-list mailing list