Anti Vowel Strange Behavior


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/antivowel?action=lesson_resume

There is no actual error, but the vowel removal of the “o” in the word “words” is not taking place and I cannot figure out why. When I input the phrase “Hey look words there man!” the function returns “Hy lk words thr mn!”

I expect the function to return “Hy lk wrds thr mn!”



def anti_vowel(text):
	vowels = ['a','e','i','o','u']
	text = list(text)
	for letter in text:
		if letter.lower() in vowels:
			text.remove(letter)
	new_text = ''.join(text)
	return new_text
  
print (anti_vowel("Hey look words there man!")) 



#2

What does that do to the iteration sequence?


#3

Thanks, I figured it out. I ended up iterating over a shallow copy of the text list and that resolved the problem. However, based on the logic I read about, I would have expected the output to be “Hey lok wrds thr mn!” because of the two consecutive vowels in the word “look”.


#4

remove is not selective, but takes the first thing it encounters from the left. Even though one of the o’s in look was skipped, it was still the o that was removed when the o in words came up in the loop.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.