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

+1

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

12 Answers

New Answer

+8

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

+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

+3

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

+2

I got one! word=input() word=list(word) vowels=['a','e','i','o','u'] left=[l for l in word if not l in vowels] print(left)

+1

word = input() vowels=["A","a","E","e","I","i","O","o","U","u"] key=[] for x in word: if x not in vowels: key.append(x) print(key) This will work

0

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

0

vowels = ['a', 'e', 'i', 'o', 'u'] word = input() word = list(word) result = [letter for letter in word if not letter in vowels] print(result)

0

here's my solution (similar to a few here) but in only 4 lines : word = input() vowels = ['a', 'e', 'i', 'o', 'u'] cons = [w for w in word if w not in vowels] print(cons)

0

Here an example based in ideas above https://code.sololearn.com/c9XHw9Ci1Dzd/?ref=app

0

Makes no sense to post wrong answers

0

Remember range will ouput intergers

0

Lukia Shaw This is the answer word=input() word=list(word) vowels=['a','e','i','o','u'] left=[l for l in word if not l in vowels] print(left) But i wonder why this doesnt work: word=input() word=list(word) left=[l for l in word if I != [‘a’,’e’,’i’,’o’,’u’] print(left)