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 Python Collections (2016, retired 2019) Lists Disemvowel

Disemvowel - Getting right answer but no dice

Tried the following, which returns the right string, but I'm getting an error that says "got back letters I wasn't expecting."

What should I be doing differently?

disemvowel.py
def disemvowel(word):
    vowels = ["a","e","i","o","u"]
    word_list = list(word.lower())
    ind=0
    for letter in word:

        if word_list[ind] in vowels:

            del word_list[ind]
            word = ''.join(word_list)
        else:
            ind += 1

    return word

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

By lowering the entire word, non-vowels are also lower-cased. This is not desired.

Instead of lowering the whole word, lower only the letter being compared. With this simple switch, your code passes.

def disemvowel(word):
    vowels = ["a","e","i","o","u"]
    word_list = list(word)  # <-- removed .lower() from here
    ind=0
    for letter in word:

        if word_list[ind].lower() in vowels:  # <-- added .lower() here

            del word_list[ind]
            word = ''.join(word_list)
        else:
            ind += 1

    return word
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Additionally, there is no need to rejoin word in each loop. The way that the code del characters from the word_list, the resulting rejoined would be equivalent to the remaining indexed characters from word in the for statement. So the join can be move to the end and run once thus making the code more efficient.

def disemvowel(word):
    vowels = ["a","e","i","o","u"]
    word_list = list(word)  # <-- removed .lower() from here
    ind=0
    for letter in word:

        if word_list[ind].lower() in vowels:  # <-- added .lower() here

            del word_list[ind]

        else:
            ind += 1
    word = ''.join(word_list)  ## <-- move the join here to do the final join once
    return word
Steven Parker
Steven Parker
231,236 Points

There's some unusual aspects to this code worth mentioning.

While the code works (with Chris's modification) there's some rather strange things going on here. For one, you iterate using word but then modify word inside the loop. Normally doing this kind of thing would cause elements to get skipped over.

But on the other hand your iterator is lettter, but letter is never used in (or after) the loop. Not illegal but very peculiar! I guess in this case two potential problems actually cancel each other out!

So while you pass the challenge, you also point out two good programming practices (by violating them both!):

  1. never modify the source of iteration inside the loop
  2. if you create but never use an iterator, you should probably use a different kind of loop

I'd be inclined to give you a prize for cleverness :trophy: along with an admonition of "don't do that again!" :stuck_out_tongue_winking_eye:

Steven Parker
Steven Parker
231,236 Points

Note that Chris's 2nd mod fixes item 1.

Chris, thank you! I was able to figure it out.

Steven - Understood on both points, and appreciate the feedback.

Thanks for the speedy responses