Skip to content

Instantly share code, notes, and snippets.

@bbookman
Created December 26, 2018 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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, " "']
@gonzalezloaizar
Copy link

This could fail if the sentence contains special characters.

@Alvaro8317
Copy link

Why could fail? Works very well for me like this:

stringex4 = "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams" print(list (a for a in stringex4 if a not in ('a','e','i','o','u', ' ') ) )

And I have a similar code

@gonzalezloaizar
Copy link

Add special characters to your stringex4 and verify that it is going to add the special characters like they were consonants.

@Alvaro8317
Copy link

@gonzalezloaizar You're right, I understand, complementing the solution of the author, I added to the conditions this to support special characters:
import string
stringex4 = "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams, special characters: * \' and ()/"
print(list (a for a in stringex4 if a not in ('a','e','i','o','u', ' ') and (a in list(string.ascii_lowercase) or a in list(string.ascii_uppercase)) ) )

@gonzalezloaizar
Copy link

Great 😃👍🏻

@sajidahmedkhan
Copy link

List Comprehension: Consonants in a string

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

@nitin-4444
Copy link

Try this one

string="Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams, special characters: * ' and ()/"
print([char for char in string if ('A'<=char<='Z' or 'a'<=char<='z') and char not in 'aeiouAEIOU'])

It will work for all kind of string including numeric and special characters

@akshayshukla1991
Copy link

akshayshukla1991 commented Oct 7, 2022

What is wrong with my code below?

text = 'Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams'
vowel = ['a','e','i','o','u']
p = [i for i in text for v in vowel if v not in i.lower()]
print(p)

@JasonHertzog
Copy link

JasonHertzog commented Jan 17, 2023

What is wrong with my code below?

text = 'Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams' vowel = ['a','e','i','o','u'] p = [i for i in text for v in vowel if v not in i.lower()] print(p)

Hey there, let's look at this together!

Basically, you're saying "for each character in text, for each vowel in vowels, if the vowel is not in the lowercase version of the already lower-case vowels then add it to my list, p.

There's really two things we want to change here.

First, we only need the "for each character in text..." portion. Let's update only this for now:
p = [i for i in text if i not in i.lower()]

Okay sweet, so now if the character i is not in i.lower it'll add it to the list, p. Of course, this doesn't really make sense, so we need to change the last part. Let's break it down.
So i here is being used to hold the character that we are currently on as it iterates. We are wanting to check to see if this character is in your list, vowel. We also want to account for the case where the current character is uppercase, and preferably make it lowercase for our comparison. Let's do this now!
p = [i for i in text if i.lower() not in vowel]

This should now do what we want it to do!

A small side note, don't forget that this problem is asking for only the consonants. We may additionally want to ignore the spaces:
p = [i for i in text if i.lower() not in (vowel + [' '])]

I hope this helps!

@jfcampoli
Copy link

I would just propose to transforme the list in a set to have a unique count of each different consonnant :

IN [ ] :
some_string = "Yellow Yaks like yelling and yawning and yesturday they yodled while eating yuky yams"
voyelles = ["a", "e", "i", "o", "u", "y", " "]
consonnant = [c for c in some_string.lower() if c not in voyelles]
print(set(consonnant))

OUT[ ] : {'g', 'r', 'l', 'w', 'd', 'k', 'h', 's', 'm', 't', 'n'}

@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]

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