When it is required to print element with maximum vowels from a list, a list comprehension is used.
Example
Below is a demonstration of the same
my_list = ["this", "week", "is", "going", "great"] print("The list is :") print(my_list) 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 :") print(my_result)
Output
The list is : ['this', 'week', 'is', 'going', 'great'] The result is : k
Explanation
- 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.