8/15 anti_vowel, How could i shorten my code?:S


#1

def anti_vowel(text):
    vowel= []
    for a in text:
        vowel.append(a)
    for char in vowel:
        if 'e' in vowel:
            vowel.remove('e')
        if 'o'in vowel:
            vowel.remove('o')
        if 'a' in vowel:
            vowel.remove('a')
        if 'i' in vowel:
            vowel.remove('i')
        if 'u' in vowel:
            vowel.remove('u')
        if 'E' in vowel:
            vowel.remove('E')
        if 'O'in vowel:
            vowel.remove('O')
        if 'A' in vowel:
            vowel.remove('A')
        if 'I' in vowel:
            vowel.remove('I')
        if 'U' in vowel:
            vowel.remove('U')
    return ''.join(vowel)

It works, but i'm thinking its a bit too long


#2

Hi,

one way would be:

def anti_vowel(text):
    vowel_free = []
    for letter in text:
        if letter not in "aeiouAEIOU":
            vowel_free.append(letter)
    return "".join(vowel_free)

If you are familiar with list comprehensions:

def anti_vowel(text):
    return "".join([letter for letter in text if letter not in "aeiouAEIOU"])

#3

and their many variations...

def anti_vowel(text):
    return ''.join([c for c in text if c.lower() not in 'aeiou'])

print (anti_vowel("Mississippi"))     # Msssspp

For anybody who is really daring, there are regular expressions, and the .sub() method.

import re
def no_vowel(text):
    return re.sub(u'[aeiou]','',text, flags=re.I)
print (no_vowel("Toledo, Ohio"))     # Tld, h

Is this code fast?
#4

Thanks!
:wink:
:smile:


#6