mapping functions and lambda

S

Suresh Jeevanandam

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?

regards,
Suresh
 
S

Steve Holden

Suresh said:
I got it:
dict([k.split('=') for k in s.split(',')])

Suresh said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,284
Messages
2,571,414
Members
48,106
Latest member
JamisonDev

Latest Threads

Top