2

I'm trying to solve a challenge on Codewars.

Trolls are attacking your comment section!

A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat.

Your task is to write a function that takes a string and return a new string with all vowels removed.

For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!".

Note: for this kata y isn't considered a vowel.

This is working:

def disemvowel(string):
    output = []
    for char in string:
        if char not in "aeiouAEIOU":
            output.extend(char)
    return "".join(output)

But I want to do it in one line with a list comprehension. I tried this:

return "".join([[].extend(char) for char in string if char not in "aeiouAEIOU"])

... but I'm getting

TypeError: sequence item 0: expected str instance, NoneType found

CDJB
  • 14,043
  • 5
  • 29
  • 55
itsame
  • 171
  • 4
  • 14
  • related: https://stackoverflow.com/questions/21581824/correct-code-to-remove-the-vowels-from-a-string-in-python – NoDataDumpNoContribution Feb 11 '20 at 13:10
  • Also related, seems to be a similar issue: https://stackoverflow.com/questions/18852324/typeerror-sequence-item-0-expected-string-nonetype-found – AMC Feb 11 '20 at 20:40

2 Answers2

4

You're trying to make a list within your list-comprehension; you can just use the existing list:

return "".join([char for char in x if char not in "aeiouAEIOU"])

Note that we could even omit the list comprehension and just use a generator expression (by omitting the square brackets), but join() works internally by converting the sequence to a list anyway, so in this case using a list-comprehension is actually quicker.

CDJB
  • 14,043
  • 5
  • 29
  • 55
0
def disemvowel(string_):
    vowels = 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'
    for a in vowels:
      string_ = string_.replace(a, '')
    return string_
Zhoma
  • 1