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


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096


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:
        text_list.append(char)
    print text_list
    
    for i in vowels:
        for char in text_list:
            if char == i:
                print char," Removed"
                text_list.remove(char)
    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?
#2

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.


#3

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


#4

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


#5

Ahh got it, really appreciate your help!


#6

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


#7

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.


#8

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


#10

A post was split to a new topic: Anti vowel


#11

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