Find words starting with a Vowel in a List in Python

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2022

banner

Photo from Unsplash

Find words starting with a Vowel in a List in Python #

To find the words that start with a vowel in a list:

  1. Use a for loop to iterate over the list.
  2. Check if the first character of each word is a vowel.
  3. Append the words that start with a vowel to a new list.
main.py
my_list = ['one', 'two', 'age', 'hello', 'example'] vowels = 'aeiouAEIOU' starting_with_vowel = [] for word in my_list: if word[0] in vowels: starting_with_vowel.append(word) print(starting_with_vowel) # 👉️ ['one', 'age', 'example']

We used a for loop to iterate over the list.

On each iteration, we access the first character in the string and check if it is a vowel.

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

main.py
vowels = 'aeiouAEIOU' print('avocado'[0] in vowels) # 👉️ True print('bobbyhadz'[0] in vowels) # 👉️ False

If the word starts with a vowel, we append it to a new list.

main.py
my_list = ['one', 'two', 'age', 'hello', 'example'] vowels = 'aeiouAEIOU' starting_with_vowel = [] for word in my_list: if word[0] in vowels: starting_with_vowel.append(word) print(starting_with_vowel) # 👉️ ['one', 'age', 'example']

The list.append() method adds an item to the end of the list.

Alternatively, you can use a list comprehension.

Find words starting with a Vowel in a List in Python #

To find the words that start with a vowel in a list:

  1. Use a list comprehension to iterate over the list.
  2. Check if the first character of each word is a vowel.
  3. The new list will only contain the words that start with a vowel.
main.py
my_list = ['one', 'two', 'age', 'hello', 'example'] vowels = 'aeiouAEIOU' starting_with_vowel = [word for word in my_list if word[0] in vowels] print(starting_with_vowel) # 👉️ ['one', 'age', 'example']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the word starts with a vowel and return the result.

The new list only contains the words that start with a vowel.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.