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

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,271 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.