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

Creating a dictionary from 2 lists with same keys

as i m beginner in python.
I have below 2 lists:


list1 =['abc','cbd','efg']
list2['applicationName','appllicationName','appllicationName']

output i am expected like below:

[{'appllicationName':'abc'},{'appllicationName':'cbd'},{'appllicationName':'efg'}]

using dict,zip function i am getting only below output:

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

{'appllicationName':'efg'}

please help me resolve this.

>Solution :

I’m not sure about what you want. But I assume you need a list of 3 single dictionaries. But @Manuel ‘s answer with one dictionary seems better to me.

Anyway… Sticking to your example code you can do it like this.

#!/usr/bin/env python3
list1 = ['abc', 'cbd', 'efg']
list2 = ['applicationName', 'appllicationName', 'appllicationName']

# Method 1
result_one = [{key: val} for key, val in zip(list2, list1)]

# Method 2 assumes that the key name is always the same.
key = 'applicationName'
result_two = [{key: val} for val in list1]

Both methods give you

[{'applicationName': 'abc'}, {'applicationName': 'cbd'}, {'applicationName': 'efg'}]
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