Python Program that Extract words starting with Vowel From A list
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)) |
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
# 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)) |
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
#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)) |
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
# 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)) |
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
# 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)) |
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
# 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)) |
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 # 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. |
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" ] # 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)) |
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)