Python Practice › List Programs › Program 96
Word lengths as a dictionary
Write a Python program to build a dictionary of each word and its length.
Input
["Hello", "student", "are", "learning", "Python", "Its", "Language"]
Output
{'Hello': 5, 'student': 7, 'are': 3, 'learning': 8, 'Python': 6, 'Its': 3, 'Language': 8}Approach
- A dict comprehension.
Solution
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.