Python Program that Extract words starting with Vowel From A list
Last Updated :
20 Feb, 2023
Given a list with string elements, the following program extracts those elements which start with vowels(a, e, i, o, u).
Input : test_list = ["all", "love", "get", "educated", "by", "gfg"]
Output : ['all', 'educated']
Explanation : a, e are vowels, hence words extracted.
Input : test_list = ["all", "love", "get", "educated", "by", "agfg"]
Output : ['all', 'educated', 'agfg']
Explanation : a, e, a are vowels, hence words extracted.
Method 1 : Using startswith() and loop
In this, we check for each word and check if it starts with a vowel using startswith() on the first alphabet of every word. The iteration part is done using the loop.
Python3
# initializing list
test_list = ["all", "love", "and", "get", "educated", "by", "gfg"]
# printing original list
print("The original list is : " + str(test_list))
res = []
vow = "aeiou"
for sub in test_list:
flag = False
# checking for begin char
for ele in vow:
if sub.startswith(ele):
flag = True
break
if flag:
res.append(sub)
# printing result
print("The extracted words : " + str(res))
OutputThe original list is : ['all', 'love', 'and', 'get', 'educated', 'by', 'gfg']
The extracted words : ['all', 'and', 'educated']
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 2 : Using any(), startswith() and loop
In this, we check for vowels using any(), and rest all the functionality is similar to the above method.
Python3
# initializing list
test_list = ["all", "love", "and", "get", "educated", "by", "gfg"]
# printing original list
print("The original list is : " + str(test_list))
res = []
vow = "aeiou"
for sub in test_list:
# check for vowel beginning
flag = any(sub.startswith(ele) for ele in vow)
if flag:
res.append(sub)
# printing result
print("The extracted words : " + str(res))
OutputThe original list is : ['all', 'love', 'and', 'get', 'educated', 'by', 'gfg']
The extracted words : ['all', 'and', 'educated']
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method 3 : Using find() method
Python3
#Starting with vowels
# initializing list
test_list = ["all", "love", "and", "get", "educated", "by", "gfg"]
# printing original list
print("The original list is : " + str(test_list))
def vowelstart(s):
if(s[0]=="a" or s[0]=="e" or s[0]=="i" or s[0]=="o" or s[0]=="u"):
return True
return False
res = []
for sub in test_list:
if(vowelstart(sub)):
res.append(sub)
# printing result
print("The extracted words : " + str(res))
OutputThe original list is : ['all', 'love', 'and', 'get', 'educated', 'by', 'gfg']
The extracted words : ['all', 'and', 'educated']
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 4 : Using for loop
Python3
# Python Program that Extract words starting
# with Vowel From A list initializing list
test_list = ["all", "love", "and", "get", "educated", "by", "gfg"]
# Printing original list
print("The original list is : " + str(test_list))
res = []
vow = "aeiou"
for i in test_list:
if i[0] in vow:
res.append(i)
# Printing result
print("The extracted words : " + str(res))
OutputThe original list is : ['all', 'love', 'and', 'get', 'educated', 'by', 'gfg']
The extracted words : ['all', 'and', 'educated']
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #5: Using filter(),list(),lambda functions and dictionary
Python3
# Starting with vowels
# initializing list
test_list = ["all", "love", "and", "get", "educated", "by", "gfg"]
vowels = {
"a": 0, "e": 0, "i": 0, "o": 0, "u": 0}
# printing original list
print("The original list is : " + str(test_list))
res = list(filter(lambda x: x[0] in vowels.keys(), test_list))
# printing result
print("The extracted words : " + str(res))
OutputThe original list is : ['all', 'love', 'and', 'get', 'educated', 'by', 'gfg']
The extracted words : ['all', 'and', 'educated']
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #6: Using operator.countOf() method
Python3
# Python Program that Extract words starting
# with Vowel From A list initializing list
import operator as op
test_list = ["all", "love", "and", "get", "educated", "by", "gfg"]
# Printing original list
print("The original list is : " + str(test_list))
res = []
vow = "aeiou"
for i in test_list:
if op.countOf(vow, i[0]) > 0:
res.append(i)
# Printing result
print("The extracted words : " + str(res))
OutputThe original list is : ['all', 'love', 'and', 'get', 'educated', 'by', 'gfg']
The extracted words : ['all', 'and', 'educated']
Time Complexity: O(N)
Auxiliary Space : O(N)
Method#7: Using regular expression
Python3
import re
# initializing list
test_list = ["all", "love", "and", "get", "educated", "by", "gfg"]
# printing original list
print("The original list is : " + str(test_list))
vow = "aeiou"
res = [x for x in test_list if re.match(f"^[{vow}]", x)]
# printing result
print("The extracted words : " + str(res))
#This code is contributed by Vinay Pinjala.
OutputThe original list is : ['all', 'love', 'and', 'get', 'educated', 'by', 'gfg']
The extracted words : ['all', 'and', 'educated']
Time Complexity: O(N)
Auxiliary Space : O(N)
Method#8: Using list comprehension and 'in'
Here's another approach using a list comprehension and checking if the first character is a vowel using in operator:
Python3
test_list = ["all", "love", "and", "get", "educated", "by", "gfg"]
# Printing original list
print("The original list is : " + str(test_list))
res = [word for word in test_list if word[0] in "aeiou"]
# Printing result
print("The extracted words : " + str(res))
OutputThe original list is : ['all', 'love', 'and', 'get', 'educated', 'by', 'gfg']
The extracted words : ['all', 'and', 'educated']
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Extract words starting with K in String List - Python In this article, we will explore various methods to extract words starting with K in String List. The simplest way to do is by using a loop.Using a LoopWe use a loop (for loop) to iterate through each word in the list and check if it starts with the exact character (case-sensitive) provided in the v
2 min read
Python Program to Accept the Strings Which Contains all Vowels The problem is to determine if a string contains all the vowels: a, e, i, o, u. In this article, weâll look at different ways to solve this.Using all()all() function checks if all vowels are present in the string. It returns True if every condition in the list comprehension is met.Pythons = "Geeksfo
2 min read
Python Regex - Program to accept string starting with vowel Prerequisite: Regular expression in PythonGiven a string, write a Python program to check whether the given string is starting with Vowel or Not.Examples: Input: animal Output: Accepted Input: zebra Output: Not Accepted In this program, we are using search() method of re module.re.search() : This me
4 min read
Python Program to Count the Number of Vowels in a String In this article, we will be focusing on how to print each word of a sentence along with the number of vowels in each word using Python. Vowels in the English language are: 'a', 'e', 'i', 'o', 'u'. So our task is to calculate how many vowels are present in each word of a sentence. So let us first des
10 min read
Python Program to print element with maximum vowels from a List Given a list containing string elements, the task is to write a Python program to print a string with maximum vowels. Input : test_list = ["gfg", "best", "for", "geeks"] Output : geeks Explanation : geeks has 2 e's which is a maximum number of vowels compared to other strings.Input : test_list = ["g
7 min read