mattyboo
mattyboo

Reputation: 47

list of lists to a dict, where first list is the key of the dict

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

Answers (2)

SharonShaji
SharonShaji

Reputation: 79

in one line:

{n:{'language':a,'edition':b} for n,(a,b) in enumerate(given_list)}

Upvotes: 0

Wondercricket
Wondercricket

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

Related Questions