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 trialsrikanth soma
1,572 PointsI think I'm very close can someone help me where I'm doing wrong?
def disemvowel(word):
vowels=['a','e','i','o','u','A','E','I','O','U']
word = list(word)
for letters in word:
if letters in vowels:
word.remove(letters)
str = ''.join(word)
return str
def disemvowel(word):
vowels=['a','e','i','o','u','A','E','I','O','U']
word = list(word)
for letters in word:
if letters in vowels:
word.remove(letters)
str = ''.join(word)
return str
3 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYou are very close! You've fallen into a classic error of modifying the object word
that is under iteration. When you alter the iterable object by removing items the loop reference pointers end up skipping items in list.
The simple fix is to iterate over a copy of word
so that letter removal from the actual word
doesn't affect the loop.
for letters in word.copy():
# or use slice notation which you'll learn soon
for letters in word[:]:
Post back if you need more help. Good luck!!!
srikanth soma
1,572 PointsYou mean like this?
def disemvowel(word):
vowels=['a','e','i','o','u','A','E','I','O','U']
word_copy = list(word)
for letters in word_copy:
if letters in vowels:
word_copy.remove(letters)
str = ''.join(word_copy)
return str
Chris Freeman
Treehouse Moderator 68,441 PointsThis is still removing characters from the object under iteration. Instead, try
def disemvowel(word):
vowels=['a','e','i','o','u','A','E','I','O','U']
word_copy = list(word)
word = list(word)
for letters in word_copy:
if letters in vowels:
word.remove(letters)
str = ''.join(word)
return str
ProTip: using ```python in your formatting will colorize the code for Python!
srikanth soma
1,572 PointsWhy are you iterating over word_copy and deleting from a word? word_copy and word will have same contents so, why to make an additional copy word?
Chris Freeman
Treehouse Moderator 68,441 PointsIt's true word and word_copy contain the same contents, but they are not the same object and may be altered independently of each other. This is important to allow deleting from the working copy word
while not affecting the iteration copy word_copy
. Otherwise you'll be back in the modified iterable error case.