Vor Vor - 2 years ago 139
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.


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

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download