Reputation: 750
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?
Upvotes: 3
Views: 12320
Reputation: 2812
def remove_vowels(word):
vowels = ['a', 'e', 'i', 'o', 'u']
word = list(word)
result = list()
for letter in word:
print('Looking for letter {}'.format(letter))
if letter.lower() not in vowels:
try:
result.append(letter)
except ValueError:
pass
return ''.join(result)
print(remove_vowels('perception'))
Upvotes: 0
Reputation: 1709
def remove_vowels(word):
vowels = ['a', 'e', 'i', 'o', 'u']
word = list(word)
word_new = []
for letter in word:
print('Looking for letter {}'.format(letter))
if letter.lower() in vowels:
continue
else:
word_new.append(letter)
return ''.join(word_new)
print(remove_vowels('perception'))
Upvotes: 0
Reputation: 4821
The reason why it didn't work before is during your for loop you are mutating word
which means that it will skip over an iteration whenever you delete something because that deletion resulted in each letter moving up a position. That means that if there was a deletion at position 2 then the next item is now at position 2 and the item after it is in position 3 which is where the next iteration is.
def remove_vowels(word):
vowels = ['a', 'e', 'i', 'o', 'u']
word = list(word)
print(word)
new_word = []
for letter in word:
print('Looking for letter {}'.format(letter))
if letter.lower() not in vowels:
try:
new_word.append(letter)
except ValueError:
pass
return ''.join(new_word)
print(remove_vowels('perception'))
Upvotes: 7
Reputation: 49812
As mentioned in comments, working on the element you are iterating on, is often troublesome. How about this:
Code:
def remove_vowels(word):
vowels = set('aeiou')
return ''.join(l for l in word if l not in vowels)
Test Code:
print(remove_vowels('perception'))
Results:
prcptn
Upvotes: 1