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

Carlos Marin
Carlos Marin
8,009 Points

Confusing results...

Could someone please give me a hint? I am unaware of why the results come out as they are.

Bummer: Hmm, got back letters I wasn't expecting! Called disemvowel('sqsIiQRRa') and expected 'sqsQRR'. Got back 'qIQR'
disemvowel.py
def disemvowel(word):
    list_of_letters = list(word)
    for index, letter in enumerate(list_of_letters, 0):
        if letter.lower() == "a" or "e" or "i" or "o" or "u":
            del list_of_letters[index]
    word = "".join(list_of_letters)
    return word

2 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Carlos,

You have a couple of critical issues with your code.

First, this line doesn't do what you think it does:

        if letter.lower() == "a" or "e" or "i" or "o" or "u":

You might think this code works like this:

If letter.lower() is equal to 'a', or if letter.lower() is equal to 'e', or if letter.lower() is equal to 'i', etc

But actually it works like this:

if letter.lower() is equal to 'a', or if 'e', or if 'i', etc

And since any string except the empty string is truthy in python, this is equivalent to:

if letter.lower() is equal to 'a', or if True, or if True, or if True, etc

And since in boolean logic, any value OR True is True, this is equivalent to:

if True:

Thus your if check will evaluate to True every time.

Your second issue is that you are deleting items from your iterable as you are iterating through it. To see the implication of this, let's take the following code and follow it through:

l = list('good')
for i, v in enumerate(l):
    print(f'l: {l}, i: {i}, v: {v}')
    if v == 'o':
        del l[i]

Here's the output:

l: ['g', 'o', 'o', 'd'], i: 0, v: g
l: ['g', 'o', 'o', 'd'], i: 1, v: o
l: ['g', 'o', 'd'], i: 2, v: d

As you can see, the loop finishes with a 'o' still in the list. This is because when we delete the o at index 1, the list shortens and the second o moves from index 2 to index 1. During the next iteration of the loop, the iteration index is 2 (which is now 'd') and so the second 'o' never gets seen by the loop.

Hope these hints point you in the right direction.

Cheers

Alex

Carlos Marin
Carlos Marin
8,009 Points

Here is my final solution!

def disemvowel(word):
    listed_letters = list(word)
    passed_letters = []
    for letter in word:
        if letter.lower() in 'aeiou':
            pass
        else:
            passed_letters.append(letter)
    word = "".join(passed_letters)
    return word
    Thank you Alex!!!! You're the best!

The first hint was very interesting! The second hint was a little confusing (mainly because I kept thinking that the l's were 1's. 

I have yet to see the "f" in front of a string inside a print statement like that, was that a typo?

print(f'l: {l}, i: {i}, v: {v}')

Regard,
    Carlos A. Marin