Word lengths as a dictionary
Write a Python program to build a dictionary of each word and its length.
["Hello", "student", "are", "learning", "Python", "Its", "Language"]
{'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}){'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.