Create a set of strings with their vowels removed


Set of strings named strings, and you want to create a new set called no_vowels that contains the same words as strings, but with the vowels removed from each word. Here's how the code works:

  • strings = {"apple", "banana", "cherry"}: This line defines a set named strings containing three words: "apple," "banana," and "cherry."
  • no_vowels = {''.join([char for char in word if char.lower() not in 'aeiou']) for word in strings}: This line uses a set comprehension to create the no_vowels set. It iterates over each word in the strings set and, for each word, it processes the characters in the word. It checks if each character (converted to lowercase) is not a vowel ('a', 'e', 'i', 'o', 'u'). If the character is not a vowel, it includes it in the processed word. The ''.join(...) part is used to concatenate the characters back into a single word. So, for each word in strings, this code creates a new word without vowels and adds it to the no_vowels set.
  • print(strings): This line prints the original strings set, which contains the words with vowels, to the console.
  • print(no_vowels): This line prints the no_vowels set, which contains the modified words with vowels removed, to the console.

Source Code

strings = {"apple", "banana", "cherry"}
no_vowels = {''.join([char for char in word if char.lower() not in 'aeiou']) for word in strings}
print(strings)
print(no_vowels)

Output

{'banana', 'cherry', 'apple'}
{'chrry', 'ppl', 'bnn'}

Example Programs