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

Keifer Joedicker
Keifer Joedicker
5,869 Points

Disemvowel, not sure what i'm doing wrong

...

def dism(word):

l = "AEIOUaeiou"

g = list(word)

    for i in l:

        if l in g:

            h = g.remove(l)

            return ''.join(h)

print dism("Heity")

...

I cant get it to return the word with the vowels taken out. When typed out piece by piece in shell it works perfect.

3 Answers

Laszlo Blanar
Laszlo Blanar
10,220 Points

First of all, use Python3, because Treehouse uses that version, as well. Second: your test case must have been wrong, that's why you think that youre solution is correct.

You cannot modify an iterable while you iterate over it, because it is gonna be messed up.

Try something like this:

def dism(word):
    vowels = 'aeiou'
    letters = list(word)
    length = len(letters)
    copy = letters[::-1]
    for idx, letter in enumerate(copy) :
        if letter.lower() in vowels:
            del letters[length-1-idx]
    return ''.join(letters)
Laszlo Blanar
Laszlo Blanar
10,220 Points

By the way, you can inspect it during run time in debug mode. E.g.:

python3 -m pdb dism.py
Keifer Joedicker
Keifer Joedicker
5,869 Points

I'm trying to understand this but i'm confused about a few parts. What does idx represent or do? and on the del letters[length-1-idx] what is that doing bewteen the []? I broke the code in my editor to understand it but it seems to work fine with just the 1 in bewteen the []

Laszlo Blanar
Laszlo Blanar
10,220 Points

idx stands for index. I.e. the index of the element in the letter variable.

I created a copy of the original list, to use for iteration. It does not really matter, in which direction I iterate over it, neither matters whether it looks exactly like the original, or reversed.

BUT: from the original list, I delete from the end to the beginning, because that way, the indexing is not messed up.

E.g.: if I delete something with index 6, than the indexes of the elements before that remain unchanged, so, I can still delete something with index 3, let's say.

But if I want to delete something for example with index 0, I have to do it last, because if I do it sooner, every element after that loses its original index.

I just use length-1-idx to translate the index of the copy (what I reversed, but it is an optional step), to the index of the original one, to make sure to delete from the end to the beginning (as I used left-to-right indexing).

For example, in ytieH, i has index 2, so 5-1-2 is index 2 in the original (not reversed) list. e has index 3, so it will be 5-1-3 = index 1 in Hety (I have already deleted letter i by now).

If I used right-to-left indexing (i.e. negative indexing), I would have to delete from left to right, to keep the original indexes of the elements that have not been iterated, yet.

Is the function name the function name the challenge wanted?

Keifer Joedicker
Keifer Joedicker
5,869 Points

I changed it back, but its not returning anything on my end its just returns 'None'

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you just need to tweak a few things. in your if, you want to compare your loop variable, i, not the list, l, to g. you don't need h, just g.remove, and the argument to remove is i, not l. then move your return over to the left out of the loop and you should be ok!