Python Practice › Dictionary Programs › Program 6
Dictionary from two lists
Write a Python program to create a dictionary from a list of keys and a list of values.
Input
["a", "b", "c", "d", "e"], [12, 23, 24, 25, 15, 16]
Output
{'a': 12, 'b': 23, 'c': 24, 'd': 25, 'e': 15}Approach
dict(zip(keys, values)); zip stops at the shorter list.
Solution
keys = ["a", "b", "c", "d", "e"]values = [12, 23, 24, 25, 15, 16]print(dict(zip(keys, values)))Output
{'a': 12, 'b': 23, 'c': 24, 'd': 25, 'e': 15}Line by line
Line 3
The extra value 16 is ignored because zip stops when the shorter list ends.
Learn the concept
New to this topic? Read the lesson, then come back.