Python Basics

Python How to make a dictionary from two lists?

We have two lists. And we want to create a dictionary combining values of these two lists. In such case, we can create a dictionary from two lists as following way.

Example

name_list=[“rahul”,”vijay”,”ajay”]

phone_list=[“1111″,”2222″,”3333”]

#declare an empty dictionary

phone_book={}

for idnum in range(0,len(name_list)):

phone_book.add(name_list[idnum],phone_list[idnum])

#lets print the dictionary

print(phone_book)

Output:

{

“rahul”:”1111″,

“vijay”:”2222″

“ajay”:”3333″

}