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

0

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

+3

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

+2

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

+1

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

0

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