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

iterating through word

def disemvowel(word): vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"] for letter in vowels: try: word = word.remove(letter) print "yay" except: print "nay" disemvowel("Kevin")

I know that you cant iterate through words easily but how else are you supposed to get it? I tried using replace but the challenge in Python Collections yelled at me :'(:

def disemvowel(word): vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"] for letter in word: if letter in vowels: word = word.replace(letter,"") print word disemvowel("Shut Up")

disemvowel.py
def disemvowel(word):
    vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
    for letter in vowels:
        try:
            word = word.remove(letter)
            print "yay"
        except:
            return word
            print "nay"

1 Answer

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

you can loop through strings and compare each element (each letter) however you like. then call the remove method. but watch for a common issue - modifying lists while looping through them. to avoid this, make a copy of the list and modify that while looping through the original (or vice versa). the remove method removes the stated index, then shifts the following elements left, but the loop won't account for that. this leads to skipping elements and incorrect results.