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 trialWayne Comber
8,169 PointsWhy doesnt this disemvowel code work properly?
Hi all,
I am a bit confused as to why my code doesn't work properly. It works fine for single vowels, but where there are consecutive vowels, it only picks up every second one. For example: If oTAOTJR is provided as the argument, TOTJR is returned, not TTJR as exepcted. AeAt returns eT not T oOao returns Oo not "" sALsEoErD returns sLsorD not sLsrD You get the picture......
It doesn't make a lot of sense to me because I would have thought the code should iterate through every letter, regardless of what happened to the one before.
Any help would be appreciated.
def disemvowel(word):
word_list = list(word)
for letter in word_list:
try:
if letter == "A" or letter == "E" or letter == "I" or letter == "O" or letter == "U" or letter == "a" or letter == "e" or letter == "i" or letter == "o" or letter == "u":
word_list.remove(letter)
except:
print("There was an error")
return ''.join(word_list)
4 Answers
Cheo R
37,150 PointsProblem lies with:
word_list.remove(letter)
You're iterating and deleting from the same list.
Wayne Comber
8,169 PointsThanks Cheo.
I thought that was it too, and changed to this code to avoid using the same list, and still get the same result.
def disemvowel(word):
word_list = list(word)
edited_list = word_list
for letter in word_list:
try:
if letter == "A" or letter == "E" or letter == "I" or letter == "O" or letter == "U" or letter == "a" or letter == "e" or letter == "i" or letter == "o" or letter == "u":
edited_list.remove(letter)
except IndexError:
print("There was an error")
return ''.join(edited_list)
Cheo R
37,150 Pointsedited_list = word_list
You're referencing the same list. One way to make a copy is to use [:].
Wayne Comber
8,169 PointsSo I have reviewed the video on the .remove function, and am back to being confused as to why my code doesn't work. The remove function removes an item by value, not by index. If it removed the item by index, I can see how you would get a problem with missing the right item, but it should also incorrectly remove consonants following a vowel. If you remove by value though, it should just keep iterating and remove an item based on value, and look at the next one regardless, and not miss one.