Two lists in a dict to one flat dict

EasyDictionary Programs · 52 of 94

Write a Python program to pair the two list values of a dictionary into a flat dictionary.

Advertisement
Input
{"name": ["Apr", "May", "June"], "month": [4, 5, 6]}
Output
{'Apr': 4, 'May': 5, 'June': 6}

Approach

  1. zip the two lists.

Solution

Python
d = {"name": ["Apr", "May", "June"], "month": [4, 5, 6]}print(dict(zip(d["name"], d["month"])))
Output
{'Apr': 4, 'May': 5, 'June': 6}

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs

Leave a Comment