Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!