I am writing a function that removes all vowels from a word. It looks like this:
def remove_vowels(word):
vowels = ['a', 'e', 'i', 'o', 'u']
word = list(word)
for letter in word:
print('Looking for letter {}'.format(letter))
if letter.lower() in vowels:
try:
word.remove(letter)
except ValueError:
pass
return ''.join(word)
I expect it to go through all the letters in the word, check each letter if it is in the vowels array and, if so, remove it.
However, it does not seem that it checks for all letters. For example, for the following call:
print(remove_vowels('perception'))
I am getting the following output:
Looking for letter p
Looking for letter e
Looking for letter c
Looking for letter e
Looking for letter t
Looking for letter i
Looking for letter n
prcpton
For some reason, it skips the r
, the second p
and the o
. I am getting a similar result with other words. Why is this happening?