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 trialnewpal
1,717 Pointswhat is wrong with my code?
def disemvowel(word): word_list = list(word) for letter in word_list: if letter in ["A", "a", "E", "e", "I", "i", "O", "o", "U", "u"]: word_list.remove(letter) word_string = ''.join(word_list) return word_string
def disemvowel(word):
word_list = list(word)
for letter in word_list:
if letter in ["A", "a", "E", "e", "I", "i", "O", "o", "U", "u"]:
word_list.remove(letter)
word_string = ''.join(word_list)
return word_string
3 Answers
Steven Parker
231,248 PointsWhen you alter an iterable while it is being used to control a loop, it can cause items to be skipped over. One way to avoid this is to iterate using a copy of the iterable.
newpal
1,717 PointsThank you Steven and Alexander for your answers! I tried using the original string word, but then I could not use the remove() method in the if statement.
Steven, Could you please explain how to use a copy of the iterable to solve this question?
Steven Parker
231,248 Points for letter in word_list.copy(): # or...
for letter in word_list[:]: # an unlimited slice is also a copy
newpal
1,717 PointsThank you Steven for your help! Much appreciated!
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsOr you could simply iterate on the original string
word
insteadSteven Parker
231,248 PointsSteven Parker
231,248 PointsThat would work in this case, but it's not a good practice in case the number or order of items got changed in the conversion process.
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsTrue.