Vowel count of each word
Write a Python program to count the vowels in each word and show the result as a dictionary.
"We are Learning Python Codding"
{'We': 1, 'are': 2, 'Learning': 3, 'Python': 1, 'Codding': 2}Approach
- A dict comprehension maps each word to its vowel count.
Solution
text = "We are Learning Python Codding"counts = {w: sum(ch in "aeiouAEIOU" for ch in w) for w in text.split()}print(counts){'We': 1, 'are': 2, 'Learning': 3, 'Python': 1, 'Codding': 2}Line by line
ch in "aeiouAEIOU" is True or False; sum() counts the Trues.
Learn the concept
New to this topic? Read the lesson, then come back.