Generate a list of strings with vowels removed from a sentence


This Python code takes a sentence, splits it into words, and creates a new sentence where all the vowels (both lowercase and uppercase) are removed from each word. Here's how the code works:

  • sentence = "This is a sample sentence with some vowels.": This line initializes a variable named sentence and assigns it a string containing the input sentence.
  • no_vowels = [''.join([char for char in word if char.lower() not in 'aeiou']) for word in sentence.split()]: This line of code initializes a variable named no_vowels and assigns it the result of a list comprehension.
    • for word in sentence.split(): This part sets up a loop that iterates through each word word in the sentence. It splits the sentence into words using sentence.split().
    • [char for char in word if char.lower() not in 'aeiou']: For each word in the list, this expression iterates through each character char in the word and includes the character in a new list only if it's not a vowel. It checks if the lowercase version of the character is not in the string 'aeiou'.
    • ''.join([char for char in word if char.lower() not in 'aeiou']) : This part joins the characters in the list (without vowels) back together into a single string, forming a word with the vowels removed.
    • [''.join([char for char in word if char.lower() not in 'aeiou']) for word in sentence.split()]: This is the list comprehension itself. It iterates through the words in the sentence, removes the vowels from each word, and includes the modified words in the new list.
  • print(sentence): This line of code prints the original sentence to the console.
  • print(no_vowels): This line of code prints the no_vowels list (which contains the modified words) to the console.

Source Code

sentence = "This is a sample sentence with some vowels."
no_vowels = [''.join([char for char in word if char.lower() not in 'aeiou']) for word in sentence.split()]
print(sentence)
print(no_vowels)

Output

This is a sample sentence with some vowels.
['Ths', 's', '', 'smpl', 'sntnc', 'wth', 'sm', 'vwls.']

Example Programs