I want to create a dictionary from a string that have key=value
s = "key1=value1 key2=value2 key3=value3"
print({r.split("=") for r in s})
Is it possible using dictionary comprehension? If yes, how?
CodePudding user response:
You can first split on whitespace, then split on '='
>>> dict(tuple(i.split('=')) for i in s.split())
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
CodePudding user response:
You could use map
:
>>> s = "key1=value1 key2=value2 key3=value3"
>>> d = {k: v for k, v in map(lambda i: i.split('='), s.split())}
>>> {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}