Python Program that extract words starting with Vowel From A list


When it is required to extract words starting with vowel from a list, a simple iteration, a flag value and the ‘startswith’ method are used.

Below is a demonstration of the same −

Example:

my_list = ["abc", "phy", "and", "okay", "educate", "learn", "code"]
print("The list is :")
print(my_list)
my_result = []
my_vowel = "aeiou"
print("The vowels are ")
print(my_vowel)
for index in my_list:
   my_flag = False
   for element in my_vowel:
      if index.startswith(element):
         my_flag = True
         break
   if my_flag:
      my_result.append(index)
print("The result is :")
print(my_result)

Output:

The list is :
['abc', 'phy', 'and', 'okay', 'educate', 'learn', 'code']
The vowels are
aeiou
The result is :
['abc', 'and', 'okay', 'educate']

Explanation

  • A list is defined and displayed on the console.

  • An empty list is created.

  • The vowel string is defined and displayed on the console.

  • The list is iterated over, and the flag is assigned to Boolean ‘False’.

  • If the first element of each string begins with characters in the vowels list, the Boolean flag value is set to ‘True’.

  • This is checked using the ‘startswith’ method.

  • The control breaks out of the loop.

  • If the value of Boolean flag is ‘True’, the element is appended to the empty list.

  • This is the output that is displayed on the console.

Updated on: 07-Sep-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements