List comprehension python | SoloLearn: Learn to code for FREE!


List comprehension python

Given a word as input, output a list, containing only the letters of the word that are not vowels. The vowels are a, e, i, o, u. Sample Input: awesome Sample Output: ['w', 's', 'm'] My code attempt - alpha = ['b', 'c' ,'d', 'f', 'g' ,'h', 'j', 'k', 'l' ,'m' ,'n', 'p', 'q' ,'r', 's' ,'t', 'v', 'w' 'x', 'y', 'z'] word = input() data = [word for word in alpha if word.startswith(word)] print(data) I'm not able to split this at first or I don't know what is wrong

4/15/2021 10:21:37 AM

Veena Tirmal

4 Answers

New Answer


It's [letter for letter in word if letter in alpha] List conprehension has two parts, the loop and condition, so you must loop all letters in word first -> letter for letter in word and follow with condition Another way to improve it is by declaring a list of vowels, 5 is less than 21 :D [letter for letter in word if letter not in alpha] and since you can use in for string as well, you can just simply declare vowel = "aeiou" instead of declaring a list, slightly shorter and easier to code


print([a for a in word.lower() if not a in ["a","e", "i","o","u"]])


vowel = ['a', 'e',' I ','o' ,'u'] word = input() alpha = [letter for letter in word of letter not in vowel] print(alpha)


Thanks guys, big help Hoh Shen Yien Илья Мирошник