Chris Python
Chris Python

Reputation: 5

How to find number of vowels in a list of words?

The task is to find the number of vowels in each word from list, and output the dictionary with the results.

I've tried this way:

wordsList = ["creativity", "anna", "civic", "apology", "refer", "mistress", "rotor", "mindset"]
dictionary = {}
vowels = "aeoui"
count = 0

for word in wordsList:
    for letter in word:
        if letter in vowels:
            count = count + 1
            dictionary[word] = (count)

print(dictionary)

That's at which point i am, i need help how to count for each word specifically. Thanks in advance! Expected output:

{anna:2} etc.

Upvotes: 0

Views: 331

Answers (4)

Kim
Kim

Reputation: 1660

Often things like this are done using comprehensions in Python. The following is more idiomatic Python than using for loops and counter variables, and breaks down the steps for you. (It also deals with capital letters by converting the word to lowercase before testing it.)

words = ['creativity', 'SpongbUngulator']

vowel_counts = {}
vowels = set('aeiou')
for word in words:
    lowercase_word = word.lower()
    found_vowels = [letter for letter in lowercase_word if letter in vowels]
    vowel_count = len(found_vowels)
    vowel_counts[word] = vowel_count
print(vowel_counts)

It's worth pointing out that set(lowercase_word).intersection(vowels) will tell you which unique vowels are in each word. Python sets are well worth getting to know.

Upvotes: 0

Amany
Amany

Reputation: 321

Your error is that count needs to be initializes inside the loop: you also can add a part where you count both upper case and lower case vowels.

wordsList = ["creativity", "anna", "civic", "apology", "refer", "mistress", "rotor", "mindset"]
dictionary = {}
vowels = "aeoui"

for word in wordsList:
    count = 0 # I moved it
    for letter in word:
        if letter.lower() in vowels: # to consider both upper/lower case
            count = count + 1
            dictionary[word] = (count)

print(dictionary)

Upvotes: 1

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

Try this. You were not resetting count variable to 0 at the start of checking next word. I also fixed the indentation of dictionary[word] = count

wordsList = ["creativity", "anna", "civic", "apology", "refer", "mistress", "rotor", "mindset"]
dictionary = {}
vowels = "aeoui"
count = 0

for word in wordsList:
    count = 0
    for letter in word:
        if letter in vowels:
            count = count + 1
    dictionary[word] = count

print(dictionary)
#{'creativity': 4, 'anna': 2, 'civic': 2, 'apology': 3, 'refer': 2, 'mistress': 2, 'rotor': 2, 'mindset': 2}

Upvotes: 0

blhsing
blhsing

Reputation: 106883

You can use a generator expression with a test of vowel for each letter in a word in the sum function:

dictionary = {word: sum(letter in 'aeiou' for letter in word) for word in wordsList}

dictionary becomes:

{'creativity': 4, 'anna': 2, 'civic': 2, 'apology': 3, 'refer': 2, 'mistress': 2, 'rotor': 2, 'mindset': 2}

Upvotes: 1

Related Questions