With two nested list:
list_1 = [[100, 90, 90, 85, 70], [100, 90, 90, 85, 80], [105, 100, 90, 90, 85]]
list_2 = [[1, 2, 2, 3, 4], [1, 2, 2, 3, 4], [1, 2, 3, 3, 4]]
I want to get the following result:
[{100:1,90:2,90:2,85:3,70:4},{100:1,90:2,90:2,85:3,80:4},{105:1,100:2,90:3,90:3,85:4}]
Code results:
>>> [[f'{a}:{b}' for a,b in zip(*z)] for z in zip(list_1, list_2)]
[['100:1', '90:2', '90:2', '85:3', '70:4'],
['100:1', '90:2', '90:2', '85:3', '80:4'],
['105:1', '100:2', '90:3', '90:3', '85:4']]
In my case I'm just printing it with the f'string and isn't dictionary with their keys, and values, however, the result is correct and is what I need!
CodePudding user response:
The following will do as you don't want a list of lists of strings, but a list of dicts:
[dict(zip(*z)) for z in zip(list_1, list_2)]
dict(zip(*z))
is just a shorter way of doing the comprehension {a: b for a, b in zip(*z)}
.