Python Program to print element with maximum vowels from a List
When it is required to print element with maximum vowels from a list, a list comprehension is used.
Below is a demonstration of the same
my_list = ["this", "week", "is", "going", "great"]
print("The list is :")
my_result = ""
max_length = 0
for element in my_list:
vowel_length = len([element for element in element if element in ['a', 'e', 'o', 'u', 'i']])
if vowel_length > max_length:
max_length = vowel_length
my_result = element
print("The result is :")
The list is :
['this', 'week', 'is', 'going', 'great']
The result is :
- A list of strings is defined and is displayed on the console.
- An empty string variable is created.
- A variable defined as ‘max_length’ is defined and is ‘0’ is assigned to it.
- The list is iterated over, and the list comprehension is used to check if a vowel is present.
- This is converted to a list, and its length is assigned to a variable.
- If length of this list is greater than the ‘max_length’, they are equated.
- The element is assigned as the output.
- This is displayed as output on the console.
Published on 16-Sep-2021 08:42:08