Length of each value

EasyDictionary Programs · 64 of 94

Write a Python program to map each string value to its length.

Advertisement
Input
{1: "sqa", 2: "tools", 3: "python"}
Output
{'sqa': 3, 'tools': 5, 'python': 6}

Approach

  1. {v: len(v) for v in d.values()}.

Solution

Python
d = {1: "sqa", 2: "tools", 3: "python"}print({v: len(v) for v in d.values()})
Output
{'sqa': 3, 'tools': 5, 'python': 6}
Advertisement

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs