How do you convert two lists into a dictionary?

0 votes

I have two lists:

keys = ['Name','Emp ID','Contact Info']
values = ['Ken','ED445','#########']

Suggest me an easier way to convert the above given list  into below given format:

dict = {'Name' : 'Ken', 'Emp ID' : ED445, 'Contact Info' : '#########'}
May 27 in Python by Shabnam
• 600 points
8 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

There are three different ways of converting two lists into key:value pair dictionary. 

Lets look at them one by one (using above given example):

1) Naive method:

 By simply declaring a dictionary and then running a nested loop for both lists and assigning key:value pairs.

#initializing lists

keys = ['Name','Emp ID','Contact Info']
values = ['Ken','ED445','#########']

#to convert list into dictionary

res = {} 
for key in keys: 
    for value in values: 
        res[key] = value 
        values.remove(value) 
        break  

#print dictionary

print ("dict : " +  str(res)) 

Output:

dict : {'Name': 'Ken', 'Emp ID': 'ED445', 'Contact Info': '#########'}

2) Using zip() method

Here one list elements are paired with the another list elements at corresponding index in key:value pair.

# initializing lists

keys = ['Name','Emp ID','Contact Info']
values = ['Ken','ED445','#########']   

#using zip() convert list into dictionary

res = dict(zip(keys, values))

#print dictionary

print ("dict : " +  str(res)) 

Output:

dict : {'Name': 'Ken', 'Emp ID': 'ED445', 'Contact Info': '#########'}
3) Using dictionary comprehension

# initializing lists

keys = ['Name','Emp ID','Contact Info']
values = ['Ken','ED445','#########']   

#using dictionary comprehension to convert list into dictionary

res = {keys[i]: values[i] for i in range(len(keys))}

#print dictionary

print ("dict : " +  str(res)) 

Output:

dict : {'Name': 'Ken', 'Emp ID': 'ED445', 'Contact Info': '#########'}


2nd and 3rd method are faster and time saving as it involves less code.

answered May 27 by Taj
• 720 points

Related Questions In Python

0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
488 views
0 votes
1 answer

How do you get the logical xor of two variables in Python?

If you're already normalizing the inputs to ...READ MORE

answered May 29, 2018 in Python by ariaholic
• 7,320 points
1,045 views
0 votes
1 answer

How do you append to a file?

with open("test.txt", "a") as myfile: myfile.write("appended text" ...READ MORE

answered Jul 27, 2018 in Python by Priyaj
• 56,140 points
21 views
0 votes
1 answer

How do I sort a dictionary by value?

It is not possible to sort a ...READ MORE

answered Jul 30, 2018 in Python by Priyaj
• 56,140 points
16 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
63 views
+1 vote
1 answer

How can I reverse list in Python?

Reversing a list is a commonly used ...READ MORE

answered May 13 in Python by Taj
• 720 points
23 views
0 votes
1 answer

How to put time delay in Python?

You can use sleep() method to sleep ...READ MORE

answered May 28 in Python by Taj
• 720 points
14 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.