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 trialJoshua Howard
12,798 PointsUnable to get ‘disemvowel’ function to work.
Maybe i’m having a slow day. Maybe i’m just not thinking correctly about this problem. But i’m not sure why this is not working. Is it because I can’t use the join function on a string that i’ve already used the split function on?
def disemvowel(word):
vowels = ['A','a','E','e','I','i','O','o','U','u']
letters = word.split()
for letter in letters:
if letter in vowels:
letters.remove(letter)
return ''.join(letters)
3 Answers
Jennifer Nordell
Treehouse TeacherHi there! Nope, that's not the reason. I'd suggest trying this in a workspace and send in the string "Treehouse". You'll get back some interesting results. This is because you're mutating the thing you're iterating over. Every time something is removed the check for the next letter will be skipped entirely.
Hope this hint helps!
Joshua Howard
12,798 PointsThanks guys, I fixed it by using ‘list’ instead of split. Afterwards, looped through each iterative and appended the non-vowel characters into a new list.
Then I joined the list and returned that result.
Joshua Howard
12,798 PointsIn the future, what would be a good way to go about getting it to continue looping until all vowels are removed without setting the length?
Chris Freeman
Treehouse Moderator 68,441 PointsFor performance, a single linear pass-through is the best you can hope for. This can be done by iterating over a copy of letters
so that any removals from the original do not affect the loop progression. There is a python idiom using slices you'll learn later in the course. Both of these work equally well:
# using built in copy method
for letter in letters.copy():
# using slice notation
for letter in letters[:]:
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsThis, and the word passed in has no spaces so
.split()
has no effect. Try usinglist(word)
to generate a list from a string.Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherAh good catch, Chris Freeman