Vor Vor - 5 months ago 18
Python Question

How to split a string within a list to create key-value pairs in Python

I have a list that looks like this:

[ 'abc=lalalla', 'appa=kdkdkdkd', 'kkakaka=oeoeoeo']


And I want to split this list by '=' so that everything on the left side will become keys and on the right, values.

{
'abc':'lalalla',
'appa':'kdkdkdkd',
'kkakaka':'oeoeo'
}

Answer
a = [ 'abc=lalalla', 'appa=kdkdkdkd', 'kkakaka=oeoeoeo']
d = dict(s.split('=') for s in a)
print d


Output:
{'kkakaka': 'oeoeoeo', 'abc': 'lalalla', 'appa': 'kdkdkdkd'}

http://codepad.org/bZ8lGuHE

Comments