Anti_vowel : Code not working for 'oo' or other double vowels


My code generally works fine for removing vowels. However, when the argument string contains a word with two of the same vowel next to each other, such as "look" it will only remove the first vowel, giving "lok". This is made more confusing if more of the same vowel appear later in the string. For example, "look on" returns "lk on".

This issue only appear when vowels are next to each other. For example "there isn't any explanation for this!", with multiple i's and e's removes all vowels correctly.

Thinking through the iteration that is taking place I can't understand how this would happen, any thoughts?

My initial idea was that because that character was removed, the next character would be moved to the index of the previous and so would be skipped. However, the "look on" returning "lk on" example shows this can't be the case.

I'd really appreciate any feedback! I understand this can probably be approached numerous other ways but I think it would be a great learning point to understand why this is happening. Thanks in advance!

def anti_vowel(text):
    vowels = ['A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u']
    text_list = []
    for char in text:
    print text_list
    for i in vowels:
        for char in text_list:
            if char == i:
                print char," Removed"
    print text_list
    text_no_vowels = ""
    for char in text_list:
        text_no_vowels += char
    print text_no_vowels
    return text_no_vowels
anti_vowel("Look over there on the hill!")

A long winded road to anti_vowel?

this thought is correct. this is exactly what is happening, because you are looping over the same list as you are removing from.

you just need to realize that remove() will remove the first instance it can find.


Big thanks for confirming this thought!

However, I'm still confused as to why "look on" returns "lk on" though, rather than "lok n". Would you be able to talk through the steps?

In my head it goes;
1. Compare 'o' to char in text_list
2. Finds L**o**ok .... at index 1
3. Removes 'o'
4. List is now Lok ....
5. Continues from next index 'k' at index 2


because remove() will remove the first o it can find in the string. so when it encounters the o of on, a o needs to be removed, which would be the one from lok


Ahh got it, really appreciate your help!


personally, i wouldn't use remove() to solve this problem. Did you figure out how to solve it?


In the end I gave in, checked the hint and scanned the forum. It gave me the idea to build up from nothing rather than remove characters. I ended up with this;

def anti_vowel(text):
    no_vowels = ""
    for char in text:
        if char not in "aeiouAEIOU":
            no_vowels += char
    print no_vowels
    return no_vowels

anti_vowel("Look on the hill, is this working?")

Still, it was a useful lesson to learn about using loops and remove.

It's both frustrating and satisfying how tidy the final code looks.


this is a much better approach. Well done :slight_smile:


A post was split to a new topic: Anti vowel


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.