Reputation: 47
the list is like this
['language', 'edition']
['Pascal', 'third']
['Python', 'fourth']
['SQL', 'second']
and i would like the dict to have 'language' and 'edition' as keys where the other lists would be values, see desired output
{"language": "Pascal","edition": "third"},{"language":"Python","edition":"fourth"},{"language": "SQL","edition":"second"}
Upvotes: 0
Views: 47
Reputation: 79
in one line:
{n:{'language':a,'edition':b} for n,(a,b) in enumerate(given_list)}
Upvotes: 0
Reputation: 7872
You can use a combination of dict
and zip
items = [['language', 'edition'], ['Pascal', 'third'], ['Python', 'fourth'], ['SQL', 'second']]
keys = items[0]
options = items[1:]
my_dict = [dict(zip(keys, k)) for k in options]
print(my_dict)
>>> [{'language': 'Pascal', 'edition': 'third'}, {'language': 'Python', 'edition': 'fourth'}, {'language': 'SQL', 'edition': 'second'}]
Upvotes: 1