Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create dictionary from 3 lists – python

Looking for some help in creating a dictionary using 3 python lists

a = ['alpha','bravo','charlie']
b = ['a','b','c']
c = [1,2,3]

output:
{'alpha': {'letter': 'a', 'number': 1},
 'bravo': {'letter': 'b', 'number': 2},
 'charlie': {'letter': 'c', 'number': 3}}

I tried something like this. This may be close, but needs some tweaking:

{k: dict(v) for k,v in zip(a, zip(('letter', b),('number', c)))}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The dict comprehension can zip all three iterables at once, and just include a dict literal for the sub-dict:

{k: {'letter': let, 'number': num} for k, let, num in zip(a, b, c)}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading