8. Anti Vowel Help


#1

def anti_vowel(text):
new_text = " "
for i in text:
while i == 'a' or i == 'A' or i == 'e' or i == 'E' or i == 'i' or i == 'I' or i == 'o' or i == 'O' or i == 'u' or i == 'U':
new_text = text.replace(i, "")
return new_text

I think it is all indented correctly, my problem with the code is that it only replaces one vowel, so anti_vowel("Hey Here I Am") returns Hy Hr I Am


#2

You should make it easier on yourself, generate a list of the non-vowels instead of trying to work on a string.

Stings in python are immutable so convert it to a list!

Example:

def anti_vowel(text):
    end_list = []
    for item in list(text):
        if item.lower() not in 'aeiou':
            end_list.append(item)
    return ''.join(end_list)

While you can simplify the above to,

def anti_vowel(text):
    return ''.join([item for item in list(text) if item.lower() not in 'aeoiu'])

Working on immutable objects is a pain so the first thing you want to do is always convert it into something useful, and then after working on it return it back in the same format you go it.

Everything I used in both sample functions is built-in and can easily be googled so if you don't understand something as and I will point you to the resources you need to get some understanding!

As always best of luck.


#3

Hi, zeziba.

Thank you for your insights.

I have a question. I would like to work with the list a different way. Instead of appending consonants, I would like to delete the vowels. However, my code doesn't work. Python says it goes out of range. Could you take a look at it for me and detect what I am doing wrong? It goes as follows:

def anti_vowel(text):
    phrase = []
    for letter in text:
        phrase.append(letter)
    for i in range(len(phrase)):
        if phrase[i] in 'aeiouAEIOU':
            del phrase[i]
    return ''.join(phrase)

Thanks in advance.

Cheers.


#4

I do not recommend do it that way at all, not only will you be preforming unnecessary task but you are more bug prone. Not to mention you are doing way more work to accomplish the same thing.

Because you are holding the text as a local variable in your function and then creating a new variable and holding all the information again in that variable. Latter on when you are working with data bases or the such and have HUGE sets of data in the gig range doing stuff like this will cause tons of overhead that could have been easily avoided.

It is a learning process though so best of luck!


#5

Hi, zeziba. Thank you so much for your reply.

Although I understood all the downsides of coding the way I did (after all, it's much better to create a variable that holds only the things that matter than to create a variable that holds everything and then delete the items we don't want one by one), I still would like to know why Python throws an out of range error. In my head, this code would run just fine. If you could enlighten me, I'd really appreciate it.

Cheers.


#6

It's because you are modifying the list as you are looping though it,

EXAMPLE:

a = [1, 2, 3, 4, 5]
for index, item in enumerate(a):
    print(index, item)
    del a[index]

Run this code and you will see what I am talking about.


#7

Oh, I get it now! Thank you so much!