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, 2019 in Python by Shabnam
• 930 points
12,614 views

Hi,

Can we make shortest as keys and longest as values and at the same time don't miss anything?

keys = ['Ken','ED445','#########']
values = ['Name','Emp ID','Contact Info','Name2','Emp ID', 'Contact info']
EXPECTED_RESULT = {'ken': 'Name','ED445' : 'Emp ID', '#########' : 'Contact Info', 'Ken':'Name2','ED445': 'Emp ID'}

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, 2019 by Taj
• 1,080 points
When zip() method is used, dict is not callable.

Hey, @Darshika,

You can do one put square brackets while using zip() method as shown below:

res = dict[zip(keys, values)]

I hope this will remove the error you facing.

Related Questions In Python

0 votes
2 answers

How do you convert a dictionary to a defaultdict?

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

answered Aug 2, 2019 in Python by Mohammad
• 3,230 points
4,826 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do you split a list into evenly sized chunks?

Here's a generator that yields the chunks ...READ MORE

answered Dec 3, 2020 in Python by Gitika
• 65,870 points
82 views
0 votes
2 answers
+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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,970 views
0 votes
0 answers
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, 2019 in Python by Taj
• 1,080 points
96 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, 2019 in Python by Taj
• 1,080 points
4,451 views