mapping functions and lambda

Steve Holden steve at holdenweb.com
Thu Feb 16 00:21:13 EST 2006


Suresh Jeevanandam wrote:
> I got it:
> dict([k.split('=') for k in s.split(',')])
> 
> Suresh Jeevanandam wrote:
> 
>>Given a string
>>s = 'a=1,b=2'
>>
>>I want to create a dictionary {'a': '1', 'b': '2'}
>>
>>I did,
>>
>>dict(map(lambda k: k.split('='), s.split(',')))
>>
>>Is it possible to get rid of the lambda here, without having to define 
>>another function just for this.
>>
>>Is this the easiest/straight-forward way to do this?
>>

In Python 2.4 you don't even need to construct the list, you can just 
use a generator expression instead:

dict(k.split('=') for k in s.split(','))

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list