Create a set of unique vowels from a list of words
This Python code processes a list of words to extract unique vowels from those words. Here's what each part of the code does:
- words = ["apple", "banana", "cherry"]: This line defines a list called words, which contains three words: "apple," "banana," and "cherry."
- unique_vowels = {char for word in words for char in word if char.lower() in 'aeiou'}: This line uses a set comprehension to create a set called unique_vowels. Here's how it works:
- for word in words iterates through each word in the list of words.
- for char in word iterates through each character in the word.
- if char.lower() in 'aeiou' checks if the lowercase of the character is a vowel ('a', 'e', 'i', 'o', or 'u'). If it is, the character is included in the set. The set comprehension ensures that only unique vowel characters are included.
- print(words): This line prints the original list of words to the console.
- print(unique_vowels) : This line prints the set unique_vowels, which contains the unique vowel characters extracted from the words in the list.
Source Code
words = ["apple", "banana", "cherry"]
unique_vowels = {char for word in words for char in word if char.lower() in 'aeiou'}
print(words)
print(unique_vowels)
Output
['apple', 'banana', 'cherry']
{'a', 'e'}