Python Practice › Dictionary Programs › Program 64
Length of each value
Write a Python program to map each string value to its length.
Input
{1: "sqa", 2: "tools", 3: "python"}Output
{'sqa': 3, 'tools': 5, 'python': 6}Approach
{v: len(v) for v in d.values()}.
Solution
d = {1: "sqa", 2: "tools", 3: "python"}print({v: len(v) for v in d.values()})Output
{'sqa': 3, 'tools': 5, 'python': 6}Learn the concept
New to this topic? Read the lesson, then come back.