Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Nikolay Nogin
Nikolay Nogin
22,154 Points

I can't understand! My code is working but not up to the end.

It works actually, but in a very strange way, it prints 'trhuse' at the end. Why the letter 'u' is not deleted? And I've noticed that it happens to all long words. The function deleting only first 3 letters. But when I enter, for instance 'banana', with several repeated letters, it works perfect.

def disemvowel(word):
    vowels = ['a',  'e',  'i',  'o', 'u']
    word = list(word)
    print(word)
    for letter in word:
        if letter.upper().lower() in vowels:
            word.remove(letter)
    word= ''.join(word)       
    print(word)
disemvowel("trEehouse")

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

A common error is modifying the container being iterated over by the for loop. By using word.remove() it changes the indexing of the items within word thus effectively skipping a index.

Adding a print statement shows the errant flow:

>>> def disemvowel(word):
...     vowels = ['a',  'e',  'i',  'o', 'u']
...     word = list(word)
...     print(word)
...     for letter in word:
...         print("letter", letter)
...         if letter.upper().lower() in vowels:
...             word.remove(letter)
...     word= ''.join(word)
...     print(word)
...
>>> disemvowel("trEehouse")
['t', 'r', 'E', 'e', 'h', 'o', 'u', 's', 'e']
letter t
letter r
letter E
letter h
letter o
letter s
letter e
trhuse

The first lower case e is skipped due the the remove of E. The u is skipped due to the removal of the o, and the final e causes the originally skipped e to be remove due to the "first match" found by remove().

The solution is to use a copy of word in the iteration so that removals from the actual word does not effect the flow. This can be easily done by adding the slice notation: "[:]"

>>> def disemvowel(word):
...     vowels = ['a',  'e',  'i',  'o', 'u']
...     word = list(word)
...     print(word)
...     for letter in word[:]:  # <-- added slice notation
...         print("letter", letter)
...         if letter.upper().lower() in vowels:
...             word.remove(letter)
...     word= ''.join(word)
...     print(word)
...
>>> disemvowel("trEehouse")
['t', 'r', 'E', 'e', 'h', 'o', 'u', 's', 'e']
letter t
letter r
letter E
letter e
letter h
letter o
letter u
letter s
letter e
trhs
Nikolay Nogin
Nikolay Nogin
22,154 Points

Oh, i didn't know that, and I've nearly broked my brain! Thanks, Chris! It helps a lot!