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
test_list = [ "all" , "love" , "and" , "get" , "educated" , "by" , "gfg" ]
print ( "The original list is : " + str (test_list))
res = []
vow = "aeiou"
for sub in test_list:
flag = False
for ele in vow:
if sub.startswith(ele):
flag = True
break
if flag:
res.append(sub)
print ( "The extracted words : " + str (res))
|
Output
The 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
test_list = [ "all" , "love" , "and" , "get" , "educated" , "by" , "gfg" ]
print ( "The original list is : " + str (test_list))
res = []
vow = "aeiou"
for sub in test_list:
flag = any (sub.startswith(ele) for ele in vow)
if flag:
res.append(sub)
print ( "The extracted words : " + str (res))
|
Output
The 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
test_list = [ "all" , "love" , "and" , "get" , "educated" , "by" , "gfg" ]
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)
print ( "The extracted words : " + str (res))
|
Output
The 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
test_list = [ "all" , "love" , "and" , "get" , "educated" , "by" , "gfg" ]
print ( "The original list is : " + str (test_list))
res = []
vow = "aeiou"
for i in test_list:
if i[ 0 ] in vow:
res.append(i)
print ( "The extracted words : " + str (res))
|
Output
The 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
test_list = [ "all" , "love" , "and" , "get" , "educated" , "by" , "gfg" ]
vowels = {
"a" : 0 , "e" : 0 , "i" : 0 , "o" : 0 , "u" : 0 }
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda x: x[ 0 ] in vowels.keys(), test_list))
print ( "The extracted words : " + str (res))
|
Output
The 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
import operator as op
test_list = [ "all" , "love" , "and" , "get" , "educated" , "by" , "gfg" ]
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)
print ( "The extracted words : " + str (res))
|
Output
The 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
test_list = [ "all" , "love" , "and" , "get" , "educated" , "by" , "gfg" ]
print ( "The original list is : " + str (test_list))
vow = "aeiou"
res = [x for x in test_list if re.match(f "^[{vow}]" , x)]
print ( "The extracted words : " + str (res))
|
Output
The 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" ]
print ( "The original list is : " + str (test_list))
res = [word for word in test_list if word[ 0 ] in "aeiou" ]
print ( "The extracted words : " + str (res))
|
Output
The original list is : ['all', 'love', 'and', 'get', 'educated', 'by', 'gfg']
The extracted words : ['all', 'and', 'educated']
Time Complexity: O(N)
Auxiliary Space: O(N)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
20 Feb, 2023
Like Article
Save Article