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 program to find the character position of Kth word from a list of strings
Given a list of strings. The task is to find the index of the character position for the word, which lies at the Kth index in the list of strings. Examples: Input : test_list = ["geekforgeeks", "is", "best", "for", "geeks"], K = 21 Output : 0Explanation : 21st index occurs in "geeks" and point to "g
3 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
Python | Extract words from given string
In Python, we sometimes come through situations where we require to get all the words present in the string, this can be a tedious task done using the native method. Hence having shorthand to perform this task is always useful. Additionally, this article also includes the cases in which punctuation
4 min read
Python | Extract Nth words in Strings List
Sometimes, while working with Python Lists, we can have problems in which we need to perform the task of extracting Nth word of each string in List. This can have applications in the web-development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using list compreh
7 min read
Extract Keywords from a List of Strings - Python
We are given a list of strings and our task is to extract all words that are valid Python keywords. [Python keywords are reserved words that define the language's syntax (e.g., is, True, global, try).] For example: a = ["Gfg is True", "Its a global win", "try Gfg"] then the output will be: ['is', 'T
3 min read
Python program to extract characters in given range from a string list
Given a Strings List, extract characters in index range spanning entire Strings list. Input : test_list = ["geeksforgeeks", "is", "best", "for", "geeks"], strt, end = 14, 20 Output : sbest Explanation : Once concatenated, 14 - 20 range is extracted.Input : test_list = ["geeksforgeeks", "is", "best",
4 min read