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
# 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))
OutputThe 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))
OutputThe 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
OutputMaximum 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
OutputThe 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
# 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])
OutputMaximum 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
# 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])
OutputMaximum 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)
OutputMaximum 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.
Similar Reads
Python program to find N largest elements from a list
Given a list of integers, the task is to find N largest elements assuming size of list is greater than or equal o N. Examples : Input : [4, 5, 1, 2, 9] N = 2 Output : [9, 5] Input : [81, 52, 45, 10, 3, 2, 96] N = 3 Output : [81, 96, 52] A simple solution traverse the given list N times. In every tra
5 min read
Python program to find Maximum value from dictionary whose key is present in the list
Given a list with dictionary keys and a dictionary, extract maximum from dictionary values, whose key is present in list. Examples: Input : test_dict = {"Gfg": 4, "is" : 5, "best" : 10, "for" : 11, "geeks" : 3}, test_list = ["Gfg", "best", "geeks"] Output : 10 Explanation : Max value is 11, but not
6 min read
Python | Get first element with maximum value in list of tuples
In Python, we can bind structural information in form of tuples and then can retrieve the same. But sometimes we require the information of tuple corresponding to maximum value of other tuple indexes. This functionality has many applications such as ranking. Let's discuss certain ways in which this
4 min read
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", "
5 min read
Python Program to Find k maximum elements of array in original order
Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note : k is always less than or equal to n. Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their app
3 min read
Python Program to print strings with repetitive occurrence of an element in a list
Given a strings List, write a Python program that extracts all the strings with more than one occurrence of a specific value(here described using K) in elements of a list. Examples: Input : test_list = ["geeksforgeeks", "best", "for", "geeks"], K = 'e' Output : ['geeksforgeeks', 'geeks'] Explanation
5 min read
Python - Get maximum of Nth column from tuple list
Sometimes, while working with Python lists, we can have a task in which we need to work with tuple list and get the maximum of its Nth index. This problem has application in web development domain while working with data information. Letâs discuss certain ways in which this task can be performed. Me
7 min read
Python Program that displays the key of list value with maximum range
Given a Dictionary with keys and values that are lists, the following program displays key of the value whose range in maximum. Range = Maximum number-Minimum number Input : test_dict = {"Gfg" : [6, 2, 4, 1], "is" : [4, 7, 3, 3, 8], "Best" : [1, 0, 9, 3]} Output : Best Explanation : 9 - 0 = 9, Maxim
5 min read
Python | Maximum element in tuple list
Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read
Python - Row with Maximum Record Element
Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
7 min read