I have a list of lists like this :
[
["key=djbjd","val=22"],
["key=fvv","val=34"],
...
]
How can I convert it into one single dict
of the following format:
{"djbjd":"22",
"fvv":"34"
...
}
CodePudding user response:
try:
lsts = [["key=fvv","val=34"],["key=djbjd","val=22"]]
result = {i[0].split("=")[1]: i[1].split("=")[1] for i in lsts}
print(result)
output
{'fvv': '34', 'djbjd': '22'}
CodePudding user response:
Since key=
and val=
are both 4 character prefixes, you can just strip those characters off and feed the transformed list to dict()
:
>>> data = [
... ["key=djbjd","val=22"],
... ["key=fvv","val=34"],
... ]
>>> dict((i[4:] for i in t) for t in data)
{'djbjd': '22', 'fvv': '34'}