Skip to main content
1 de 3

Flatten a list of lists and get a dictionary

I would like to know if there is a way to obtain a dictionary with keys from a list of lists.

List of lists:

users = [['1043100330', 'Smith', 'John'], 
['1043100331', 'Swartz', 'Francis'], 
['1043100332', 'Laff', 'Michael']]

keys:

user_keys = ['phoneNumber', 'lastName', 'firstName']

I would like to get the following:

result=[{'phoneNumber': '1043100330', 'lastName': 'Smith, 'firstName': 'John'},
        {'phoneNumber': '1043100331', 'lastName': 'Swartz', 'firstName': 'Francis'},
        {'phoneNumber': '1043100332', 'lastName': 'Laff', 'firstName': 'Michael'},
        ]

This is my code in Python3.6:

for user in users:
    for i in users:
        d = dict(zip(user_keys, i))

print(d)

But the output is:

{'phoneNumber': '1043100332', 'lastName': 'Laff', 'firstName': 'Michael'}

All the best. Thanks!