.remove doesn't work why


#1

1 def anti_vowel(text):
2 text=list(text)
3 vowels="aeiouAEIOU"
4 for i in text:
5
6 if i in vowels:
7 print i
8
9
10 print anti_vowel("Hey look words!")

This prints all the vowels which means it reads them.

1 def anti_vowel(text):
2 text=list(text)
3 vowels="aeiouAEIOU"
4 for i in text:
5
6 if i in vowels:
7 text.remove(i)
8
9 return "".join(text)
10 print anti_vowel("Hey look words!")
but this doesn't remove the last 'o'


#2

Hi roshjha,

First a little trick: This discussion forum supports the markdown language so when you need to paste code, if you want it to retain proper formatting and indentation, put it between code syntax delimiters like such:

```
your code block here
```

this will render as:

your code block here

Alright. When stumping on any kind of error, it always helps to print debugging information to understand what's going on under the hood. Let's now look at our code with added debug print statements:

def anti_vowel(text):
    text = list(text)
    vowels = "aeiouAEIOU"
    for i in text:
        print "     We now look if \"" + str(i) + "\" is a vowel..."
        if i in vowels:
            print "         It is!"
            print "         Removing: " + str(i) + " from the 'text' list..."
            text.remove(i)
            print "         The 'text' list is now: " + str(text)
            print " "
    return text
#    
print anti_vowel("Hey look words!")

Running this code, we can now clearly see that our loop account for only one of the two o's of the word "look"!

The Python Documentation for the remove function will tell us that:

list.remove(x)
Remove the first item from the list whose value is x. It is an error if there is no such item.

What is happening next is that when our loop try to account for the o in the word "words", it's actually removing the trailing o from "look", hence your impression that the last o is not being removed.

Being a python novice myself, I am currently not entirely sure why the for loop omits one of the two o's of "look". I will therefore not pronounce myself as to not mislead you or anyone but I'm sure a more advanced user will be able to enlighten us on that matter.

What I know is that you are iterating by list item

    for i in text:

and that iterating by numerical sequence instead will work:

    for i in range(len(text)):

Doing so will also remove the need to convert the text to a list as you can adress a string by numerical index but I had to change your original approach a little because strings are immutable (cannot be changed so .remove will not work on them). Instead, I just create a new_text to store only consonants and return this as a result of my function:

def anti_vowel(text):
    new_text = ""
    vowels = "aeiouAEIOU"
    for i in range(len(text)):
        print " Looking if \"" + str(text[i]) + "\" is a vowel..."
        if text[i] not in vowels:
            print "     It is not!"
            print "     Adding: " + text[i] + " to the 'new_text' string..."
            new_text = new_text + text[i]
            print "         The 'new_text' string is now: " + new_text
            print " "
    return new_text
    
print anti_vowel("Hey look words!")

Hope this helped!


#3

Thanks for the explanation ! :smile: :smile:


#4

That was an awesome explanation! Here's an alternative sample:

def anti_vowel(text):
    vowels = "AaEeIiOoUu"
    newtext = ""
    for c in text:
        if c not in vowels:
            newtext += c
    return newtext

The trick is, as @denisaltroy said, not to try using .remove on a string because strings are immutable in Python.


#5

so i tried putting in

print anti_vowel('november')

at the end of the code and for some reason it does not work, what am i missing. took it out and it worked.


#6

@killerdevildog11: Could you post your complete code block and the error message you're getting? It will help us help you :smile:


#7

Question: I had used the same approach. But instead of “if c not in vowels:” I wrote "if c!= vowels:"
Yours worked, mine didn’t. Can somebody explain why?
Thanks in advance.