Python Practice › Dictionary Programs › Program 66
Nested dictionary from a list of keys
Write a Python program to nest each item of a dictionary under a key from a list.
Input
{"sqatools": 8, "python": 6}, [1, 2]Output
{1: {'sqatools': 8}, 2: {'python': 6}}Approach
zipthe list with the dict’s items.
Solution
d = {"sqatools": 8, "python": 6}keys = [1, 2]print({k: {name: v} for k, (name, v) in zip(keys, d.items())})Output
{1: {'sqatools': 8}, 2: {'python': 6}}Learn the concept
New to this topic? Read the lesson, then come back.