Python Practice › List Programs › Program 96

Word lengths as a dictionary

EasyList Programs · 96 of 100

Write a Python program to build a dictionary of each word and its length.

Advertisement
Input
["Hello", "student", "are", "learning", "Python", "Its", "Language"]
Output
{'Hello': 5, 'student': 7, 'are': 3, 'learning': 8, 'Python': 6, 'Its': 3, 'Language': 8}

Approach

  1. A dict comprehension.

Solution

Python
words = ["Hello", "student", "are", "learning", "Python", "Its", "Language"]print({w: len(w) for w in words})
Output
{'Hello': 5, 'student': 7, 'are': 3, 'learning': 8, 'Python': 6, 'Its': 3, 'Language': 8}

Learn the concept

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

More list programs

← All 100 List Programs

Leave a Comment