Create a dictionary of strings with vowels removed


The list of words and creates a dictionary named without_vowels. This dictionary maps each word to a version of the word where all vowels (both uppercase and lowercase) are removed. Here's how the code works:

  • words = ['apple', 'banana', 'cherry']: This line initializes the words variable with a list of words.
  • without_vowels = {word: ''.join([char for char in word if char.lower() not in 'aeiou']) for word in words}: This line uses a dictionary comprehension to create the without_vowels dictionary. It iterates through each word in the words list.
    • {word: ''.join([char for char in word if char.lower() not in 'aeiou'])}: This part of the code creates key-value pairs in the dictionary. The key is the original word, and the value is a modified version of the word. The modification is done by iterating through each character char in the word and checking if char.lower() (the lowercase version of the character) is not in the string 'aeiou', which represents the vowels. If char is not a vowel, it is included in the modified version of the word. The join function is used to concatenate the characters back into a single string.
  • print(words): This line prints the original list of words, words, to the console.
  • print(without_vowels): This line prints the without_vowels dictionary, which contains the original words as keys and their versions with the vowels removed as values.

Source Code

words = ['apple', 'banana', 'cherry']
without_vowels = {word: ''.join([char for char in word if char.lower() not in 'aeiou']) for word in words}
print(words)
print(without_vowels)

Output

['apple', 'banana', 'cherry']
{'apple': 'ppl', 'banana': 'bnn', 'cherry': 'chrry'}

Example Programs