Open In App

Python Program to print element with maximum vowels from a List

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Initializing Matrix
test_list = ["gfg", "best", "for", "geeks"]
 
# Printing original list
print("The original list is : " + str(test_list))
 
res = ""
max_len = 0
 
for ele in test_list:
 
    # Getting maximum length and element iteratively
    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
 
# Printing result
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




# Python Program to print element
# with maximum vowels from a List
 
# initializing Matrix
test_list = ["gfg", "best", "for", "geeks"]
 
# printing original list
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 = []
 
# Iterating elements in list
for ele in test_list:
    res.append(vc(ele))
a = res.index(max(res))
re = test_list[a]
 
# Printing the result
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




# Initialize the list of words
test_list = ["gfg", "best", "for", "geeks"]
 
#using dictionary comprehsion
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 the word with the maximum count of vowels
print("Maximum vowels word : " + max_vowels)
 
 
#this code contributed by tvsk


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 the re module for regular expressions
import re
 
# Initialize the test list
test_list = ["gfg", "best", "for", "geeks"]
 
# Print the original list
print("The original list is : " + str(test_list))
 
# Use the max function with a key argument to find the word with the maximum number of vowels
# The lambda function uses re.findall to find all the vowels (aeiouAEIOU) in a word
# and returns the length of the resulting list, representing the number of vowels in the word
res = max(test_list, key=lambda x: len(re.findall(r'[aeiouAEIOU]', x)))
 
# Print the word with the maximum number of vowels
print("Maximum vowels word : " + str(res))
#this code is contributed by Asif_Sahik


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:

  1. Initialize the test list 
  2. Define a function that counts the number of vowels in a given string
  3. Use the map() function to map the number of vowels in each word in the list
  4. Use the index() function to find the index of the maximum value in the resulting list
  5. Print the word with the maximum number of vowels.

Python3




# Initialize the test list
test_list = ["gfg", "best", "for", "geeks"]
 
# Define a function that counts the number
# of vowels in a given string
def count_vowels(string):
    vowels = "aeiouAEIOU"
    count = 0
     
    for char in string:
        if char in vowels:
            count += 1
    return count
 
 
# Use the map() function to map the number of vowels
# in each word in the list
vowel_counts = list(map(count_vowels, test_list))
 
# Use the index() function to find the index of the maximum value
# in the resulting list
max_index = vowel_counts.index(max(vowel_counts))
 
# Print the word with the maximum number of vowels
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.

  1. Initialize a list of words test_list containing [“gfg”, “best”, “for”, “geeks”].
  2. Create a lambda function that checks if a character is a vowel using the in operator: lambda char: char in ‘aeiouAEIOU’.
  3. 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)).
  4. 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.
  5. Assign the result of step 4 to vowels_list.
  6. Create a lambda function that finds the length of a given list: lambda l: len(l).
  7. 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.
  8. Assign the result of step 7 to vowel_count.
  9. Use the index() method to find the index of the maximum value in vowel_count.
  10. Assign the result of step 9 to max_index.
  11. Use the max_index to find the word with the maximum number of vowels in test_list.
  12. Print the word with the maximum count of vowels.

Python3




# Initialize the list of words
test_list = ["gfg", "best", "for", "geeks"]
 
# Use filter() and lambda to create a list of vowels in each word
vowels_list = list(map(lambda word: list(filter(lambda char: char in 'aeiouAEIOU', word)), test_list))
 
# Create a list of the number of vowels in each word
vowel_count = list(map(len, vowels_list))
 
# Find the index of the maximum number of vowels in the list
max_index = vowel_count.index(max(vowel_count))
 
# Print the word with the maximum count of vowels
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.

  1. 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.
  2. Initialize a list test_list containing the words to search for the maximum number of vowels.
  3. 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.
  4. Store the result of the reduce() function in a variable max_vowel_word.
  5. 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.



Last Updated : 10 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads