x = ['a', 'b', 'c']
y= [[1,2,3],[4,5,6],[7,8,9]]
I want to create a list of dictionaries so the x and y values would correspond like this:
output: [{'a':1, 'b':2, 'c':3}, {'a':4, 'b':5, 'c':6}, {'a':7, 'b':8, 'c':9}]
x = ['a', 'b', 'c']
y= [[1,2,3],[4,5,6],[7,8,9]]
I want to create a list of dictionaries so the x and y values would correspond like this:
output: [{'a':1, 'b':2, 'c':3}, {'a':4, 'b':5, 'c':6}, {'a':7, 'b':8, 'c':9}]
You can use zip
to create a dictionary from two lists: dict(zip(keys, values))
out = [dict(zip(x, l)) for l in y]
You can do it with list comprehension and enumerate that will get the corresponding value from the x list on each y iteration:
print([{x[inx]: z for inx, z in enumerate(i)} for i in y])
Output
[{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}, {'a': 7, 'b': 8, 'c': 9}]