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
• 920 points
523 views

1 answer to this question.

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
• 1,040 points

Related Questions In Python

0 votes
1 answer

How do you convert a dictionary to a defaultdict?

from collections import defaultdict a = {1: 2, ...READ MORE

answered Aug 2 in Python by Mohammad
• 3,170 points
440 views
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
842 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,340 points
4,188 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,960 points
37 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 401 views
0 votes
1 answer

How do I loop backward using indices in Python?

Hi, You can make use of negative indices ...READ MORE

answered Jul 3 in Python by Taj
• 1,040 points
31 views
0 votes
1 answer

How to create a train and test sample from one dataframe using pandas?

Hi, The below written code can help you ...READ MORE

answered Jul 3 in Python by Taj
• 1,040 points
430 views