Skip to content

Instantly share code, notes, and snippets.

@bbookman
Created December 26, 2018 21:54
Show Gist options
  • Save bbookman/7d09a66ffdec60d5a55d167751f64175 to your computer and use it in GitHub Desktop.
Save bbookman/7d09a66ffdec60d5a55d167751f64175 to your computer and use it in GitHub Desktop.
Python List Comprehension: Consonants in a string
'''
Create a list of all the consonants in the string "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams"
'''
sentence = "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams"
result = [letter for letter in sentence if letter not in 'a,e,i,o,u, " "']
@Amine-Fadssi
Copy link

Amine-Fadssi commented Jan 11, 2024

vowels_space = ['a', 'e', 'i', 'o', 'u', ' ']
sentence = 'Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams'
my_list = [letter for letter in sentence.lower() if letter not in vowels_space]

@MrCzaro
Copy link

MrCzaro commented May 8, 2024

sentence = 'Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams'
non_consonants = "aeioau"
consonants = [con for con in sentence.lower() if (con.isalpha() and con not in non_consonants)]

@Christian-Stefan
Copy link

sentence = "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams"
counting_consonants = [len([digits for digits in sentence if digits != 'a'
or digits != 'e'
or digits != 'o'
or digits != 'u'])]
print(counting_consonants)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment