Dictionary from two lists

EasyDictionary Programs · 6 of 94

Write a Python program to create a dictionary from a list of keys and a list of values.

Advertisement
Input
["a", "b", "c", "d", "e"], [12, 23, 24, 25, 15, 16]
Output
{'a': 12, 'b': 23, 'c': 24, 'd': 25, 'e': 15}

Approach

  1. dict(zip(keys, values)); zip stops at the shorter list.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs