Python: Convert 2 Lists To Dict And Repeat The Keys With Each Set Of Data
Learning about Python's zip function. I can do this list_keys = ['fname','lname','dob'] list_data = ['bob','smith','12121950'] keys_and_data = dict(zip(list_keys,list_data)) prin
Solution 1:
Loop over list_data
in a list comprehension:
keys_and_data = [dict(zip(list_keys, data)) for data in list_data]
Demo:
>>> list_keys = ['fname','lname','dob']
>>> list_data = [['bob','smith','12121950'],['john','jones','10101940']]
>>> [dict(zip(list_keys, data)) for data in list_data]
[{'lname': 'smith', 'dob': '12121950', 'fname': 'bob'}, {'lname': 'jones', 'dob': '10101940', 'fname': 'john'}]
Post a Comment for "Python: Convert 2 Lists To Dict And Repeat The Keys With Each Set Of Data"