Count Vowels in List in Python (3 Examples)
In this Python tutorial, you’ll learn how to count the total number of vowels in a list.
The tutorial is structured as follows:
Let’s dive right in!
Create Sample List
For this tutorial, we will first create a sample list. You can run the code below.
my_list = ["bicycle", "car", "metro", "plane", "bus"] print(my_list) # ["bicycle", "car", # "metro", "plane", "bus"]
The previous output prints my_list, which contains five strings. Every string in the list contains vowels, let’s see how to count these vowels!
Example 1: Count Number of Vowels in List Using List Comprehension & len() Function
In this example, we will explain how to use the list comprehension method to count the total number of vowels in a list.
This method iterates through the elements of the list and keeps those strings that are identified as vowels using the if statement
i.lower() in 'aeiou'. Then, the len() function gives us the count of total vowels in my_list.
count_vowels = len([i for x in my_list for i in x if i.lower() in 'aeiou']) print(count_vowels) # 8
In the output above we can see that the len() function counts eight vowels in my_list.
Example 2: Count Number of Vowels in List Using User-defined Function
This example shows how to create a user-defined function to count the number of vowels in a list.
As shown in the code below, the user-defined count_vowels() function will iterate through every word in our list and count each time a vowel is found by using the conditional statement
if i.lower() in vowels. The total count of vowels will be stored in the vowel_count variable.
def count_vowels(sample_list): vowels = ('aeiou') vowel_count = 0 for word in sample_list: for i in word: if i.lower() in vowels: vowel_count += 1 return vowel_count
Next, we can apply the count_vowels function to my_list to get the total number of vowels.
count_vowels(my_list) # 8
The result is eight vowels, just as expected!
Example 3: Count Number of Vowels in List Using filter(), sum() & len() Functions
It is also possible to count the vowels in a list by using the filter(), sum() and len() functions. Let’s take look at the code below!
vowels = ('aeiou') count_vowels = sum(len(list(filter(lambda i: i.lower() in vowels, word))) for word in my_list) print(count_vowels) # 8
As you can see on the previously shown output of the Python console, this method uses the filter() function to create a list of vowels for each word in the list.
The lambda function checks if a character is a vowel and the len() function gets the length of the filtered list of vowels, which is added to the total count_vowels using the sum() function. The final result is eight, great!
Video, Further Resources & Summary
Do you need more explanations on how to count the vowels in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
In addition, you may want to have a look at the related programming tutorials on Statistics Globe:
- Access List Element by Index in Python
- Count Elements Using List Comprehension in Python
- Convert List from Character String to Integer in Python
- Count True & False in List in Python
- Learn Python
This post has shown how to count vowels in a list in Python. In case you have further questions, feel free to leave a comment below.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter