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

Code Challenge 1 in Python Collections - Why doesn't this work??

I understand how to get around this for the challenge (create a new word and append to it), but I don't understand why this code isn't working?? I created a script on my macbook and I am trying to run it there. (my laptop runs 2.7, hence the raw_input function, but everything else seems to work the same as treehouse). This is the full script:

def disemvowel(word):

    lword = list(word)

    print(lword)

    for letter in lword:

        print("letter is: " + letter)

        if letter.lower() in ("a", "e", "i", "o", "u"):

            lword.remove(letter)

    return str(lword)

word = raw_input("Word: ")

new_word = disemvowel(word)

print(new_word)

When I run this code, I get the following output:

 Aniel:TeamTreehouse Anne$ python disemvowel.py
 Word: treehouse
 ['t', 'r', 'e', 'e', 'h', 'o', 'u', 's', 'e']
 letter is: t
 letter is: r
 letter is: e
 letter is: h
 letter is: o
 letter is: s
 letter is: e
 ['t', 'r', 'h', 'u', 's', 'e']

Why is it acting so weird? I can't figure out why it's skipping letters and ignoring the last letter even though it hits?

It is not safe to remove elements from list during iteration:

for letter in lword:
    lword.remove(letter)

That actually makes a lot of sense. I wouldn't free a node in a linked list in C while i was still pointing to that node in an iteration for the same reason. Thanks for the help!