LarsVegas
LarsVegas

Reputation: 6812

python: how to make the result of split directly key, value pair

If this is the desired result:

t = {'p': '011', 'or': 'artificial', 'pc': '3718'}

and this the list the dict should be made from:

s = ['p=011', 'or=artificial', 'pc=3718']

How can you write the key -value assignment and the split-function in one line? I mean something like this (which of course is not working):

t = dict()
for e in s:
    t[k] = v = k,v = e.split("=")

Upvotes: 0

Views: 875

Answers (1)

zhangyangyu
zhangyangyu

Reputation: 8610

>>> s = ['p=011', 'or=artificial', 'pc=3718']
>>> dict(x.split('=') for x in s)
{'p': '011', 'or': 'artificial', 'pc': '3718'}
>>> 

Upvotes: 6

Related Questions