Program to create a dictionary from two given list in Python
In this python programming article, we are going to learn
-
program to make a dictionary with two given list in python
Program to make a dictionary with two given lists in Python
The program is as follows:
# Owner : TutorialsInhand Author : Devjeet Roy
roll_nos = [22,36,56,78,45]
names = ["Atul","Ajay","Devjeet","Sonu","Shreya"]
db = dict()
for i,j in zip(roll_nos,names):
db[i] = j
print("The dictionary is:\n",db)
The output of the program is as follows:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
The dictionary is:
{22: 'Atul', 36: 'Ajay', 56: 'Devjeet', 78: 'Sonu', 45: 'Shreya'}
Few important tips about the program
1. From the two given list, we have used one list elements as keys and the other one as values.
2. The zip() function helps us to iterate over two iterables simultaneously.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :
Published Date :
Oct 13,2022