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

How to create a dictionary assigning values from a list and generate the same key for each one

I have a list and I’d like that each item of the list becomes the value of a key that doesn’t exist yet but should be created.

This is the list:

['King', 'President', 'VP', ' 2nd VP', '3rd VP']

The desired output must be like this:

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

[{'title':'King'}, {'title':'President'}, {'title':'VP'}, {'title':'2nd VP'}, {'title':'3rd VP'}]

Thanks for your support

>Solution :

You can do that using List Comprehension

lst = ['King', 'President', 'VP', ' 2nd VP', '3rd VP']
d_lst = [{'title': v} for v in lst]
d_lst = [{'title': 'King'}, {'title': 'President'}, {'title': 'VP'}, {'title': ' 2nd VP'}, {'title': '3rd VP'}]
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