Python Program to print element with maximum vowels from a List
Last Updated :
10 May, 2023
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 = ["gfg", "best"]
Output : best
Explanation : best has 1 e which is a maximum number of vowels compared to other strings.
Approach 1: Using loop
In this, we iterate for all the strings and keep a counter to check number of vowels in each string. Then, return the string with maximum vowels at end of the loop.
Python3
test_list = [ "gfg" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
res = ""
max_len = 0
for ele in test_list:
vow_len = len ([el for el in ele if el in [ 'a' , 'e' , 'o' , 'u' , 'i' ]])
if vow_len > max_len:
max_len = vow_len
res = ele
print ( "Maximum vowels word : " + str (res))
|
Output
The original list is : ['gfg', 'best', 'for', 'geeks']
Maximum vowels word : geeks
Time Complexity: O(n2)
Auxiliary Space: O(n)
Approach 2: Using max() and count() methods
Python3
test_list = [ "gfg" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
def vc(s):
x = s.count( "a" ) + s.count( "e" ) + s.count( "i" ) + s.count( "o" ) + s.count( "u" )
return x
res = []
for ele in test_list:
res.append(vc(ele))
a = res.index( max (res))
re = test_list[a]
print ( "Maximum vowels word : " + str (re))
|
Output
The original list is : ['gfg', 'best', 'for', 'geeks']
Maximum vowels word : geeks
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Approach 3: Using dictionary comprehension
Python3
test_list = [ "gfg" , "best" , "for" , "geeks" ]
vowel_count = {word: sum ( 1 for char in word if char in "aeiouAEIOU" ) for word in test_list}
max_vowels = max (vowel_count, key = vowel_count.get)
print ( "Maximum vowels word : " + max_vowels)
|
Output
Maximum vowels word : geeks
Time Complexity: O(n*m), where n is the number of words in the list and m is the average length of the words.
Auxiliary Space: O(n)
Approach 4: Using re module
Python3
import re
test_list = [ "gfg" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
res = max (test_list, key = lambda x: len (re.findall(r '[aeiouAEIOU]' , x)))
print ( "Maximum vowels word : " + str (res))
|
Output
The original list is : ['gfg', 'best', 'for', 'geeks']
Maximum vowels word : geeks
Time Complexity: O(n*m)
Auxiliary Space: O(n)
Approach 5: Using mao() + index()
The map() function to map the number of vowels in each word in the list and then using the index() function to find the index of the maximum value in the resulting list.
Steps:
- Initialize the test list
- Define a function that counts the number of vowels in a given string
- Use the map() function to map the number of vowels in each word in the list
- Use the index() function to find the index of the maximum value in the resulting list
- Print the word with the maximum number of vowels.
Python3
test_list = [ "gfg" , "best" , "for" , "geeks" ]
def count_vowels(string):
vowels = "aeiouAEIOU"
count = 0
for char in string:
if char in vowels:
count + = 1
return count
vowel_counts = list ( map (count_vowels, test_list))
max_index = vowel_counts.index( max (vowel_counts))
print ( "Maximum vowels word : " + test_list[max_index])
|
Output
Maximum vowels word : geeks
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), as it creates a new list to store the vowel counts.
Method 6 : using the filter() and lambda functions.
- Initialize a list of words test_list containing [“gfg”, “best”, “for”, “geeks”].
- Create a lambda function that checks if a character is a vowel using the in operator: lambda char: char in ‘aeiouAEIOU’.
- Create another lambda function that filters out all the consonants from a given word using the filter() function and the lambda function defined in step 2: lambda word: list(filter(lambda char: char in ‘aeiouAEIOU’, word)).
- Use the map() function to apply the lambda function from step 3 to each word in the test_list. This creates a list of lists, where each sublist contains the vowels of a single word.
- Assign the result of step 4 to vowels_list.
- Create a lambda function that finds the length of a given list: lambda l: len(l).
- Use the map() function to apply the lambda function from step 6 to each sublist in vowels_list. This creates a list of integers, where each integer is the number of vowels in a single word.
- Assign the result of step 7 to vowel_count.
- Use the index() method to find the index of the maximum value in vowel_count.
- Assign the result of step 9 to max_index.
- Use the max_index to find the word with the maximum number of vowels in test_list.
- Print the word with the maximum count of vowels.
Python3
test_list = [ "gfg" , "best" , "for" , "geeks" ]
vowels_list = list ( map ( lambda word: list ( filter ( lambda char: char in 'aeiouAEIOU' , word)), test_list))
vowel_count = list ( map ( len , vowels_list))
max_index = vowel_count.index( max (vowel_count))
print ( "Maximum vowels word: " + test_list[max_index])
|
Output
Maximum vowels word: geeks
Time complexity: O(n), where n is the length of the list of words.
Auxiliary space: O(n), where n is the length of the list of words (to store the list of vowel counts).
Method 7: Use the reduce() function from the functools module.
- Define a function max_vowel_word() that takes two words as input and returns the one with the maximum number of vowels. The function does the following:
a. Creates a set of vowels.
b. Calculates the vowel count for the first word by counting the number of characters in the word that are present in the vowels set.
c. Calculates the vowel count for the second word in the same way as step (b).
d. Returns the word with the maximum vowel count between the two words.
- Initialize a list test_list containing the words to search for the maximum number of vowels.
- Apply the reduce() function to all the words in test_list using the max_vowel_word() function defined in step (1). The reduce() function applies the max_vowel_word() function to each pair of words in test_list and returns the word with the maximum vowel count.
- Store the result of the reduce() function in a variable max_vowel_word.
- Print the word with the maximum vowel count stored in max_vowel_word.
Python3
from functools import reduce
def max_vowel_word(word1, word2):
vowels = set ( 'aeiouAEIOU' )
vowel_count1 = sum ( 1 for char in word1 if char in vowels)
vowel_count2 = sum ( 1 for char in word2 if char in vowels)
return word1 if vowel_count1 > = vowel_count2 else word2
test_list = [ "gfg" , "best" , "for" , "geeks" ]
max_vowel_word = reduce (max_vowel_word, test_list)
print ( "Maximum vowels word:" , max_vowel_word)
|
Output
Maximum vowels word: geeks
The time complexity of this approach is O(n), where n is the number of words in the list,
Auxiliary space complexity is O(1) as we are not using any additional data structures to store the vowel counts or words.
Share your thoughts in the comments
Please Login to comment...