Today we are going to share a Python program to map two lists into a dictionary. If you are a python beginner and want to start learning the python programming, then keep your close attention in this tutorial as I am going to share a Python program to map two lists into a dictionary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | keys=[] values=[] n=int(input("Enter number of elements for dictionary:")) print("For keys:") for x in range(0,n): element=int(input("Enter element" + str(x+1) + ":")) keys.append(element) print("For values:") for x in range(0,n): element=int(input("Enter element" + str(x+1) + ":")) values.append(element) d=dict(zip(keys,values)) print("The dictionary is:") print(d) |
Enter number of elements for dictionary:2
For keys:
Enter element1:23
Enter element2:46
For values:
Enter element1:69
Enter element2:138
The dictionary is:
{46: 138, 23: 69}
To increase your Python knowledge, practice all Python programs, here is a collection of 100+ Python problems with solutions.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.