Generate a list of strings with vowels removed


This Python code takes a list of strings, removes vowels from each string, and stores the modified strings in a new list called no_vowels. Here's how the code works:

  • strings = ["apple", "banana", "cherry"]: This line initializes a variable named strings and assigns it a list 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 of code initializes a variable named no_vowels and assigns it the result of a list comprehension.
    • for word in strings: This part sets up a loop that iterates through each word in the strings list.
    • for char in word if char.lower() not in 'aeiou': This inner loop iterates through each character (char) in the current word, but only if the lowercase version of char is not in the string 'aeiou' (i.e., it filters out vowels). The .lower() method is used to handle both uppercase and lowercase vowels.
    • ''.join(...): This part joins the filtered characters back together to form a modified word with vowels removed. ''.join(...) is used to concatenate the characters without any spaces or separators.
    • [''.join([char for char in word if char.lower() not in 'aeiou']) for word in strings]: This is the list comprehension itself. It iterates through the words in the strings list, removes vowels from each word, and includes the modified words in the new list.
  • print(strings): This line of code prints the original strings list to the console.
  • print(no_vowels): This line of code prints the no_vowels list 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

['apple', 'banana', 'cherry']
['ppl', 'bnn', 'chrry']

Example Programs