Omar Araby
Omar Araby

Reputation: 17

my code to remove vowels from a string is acting weird

So I wrote this code to remove the vowels from any string given. it should work fine. And it actually does. Just not for all strings which is weird why it would work for some strings and not for others

here's the code:

vowels = ["a", "e", "i", "o", "u"]
def anti_vowel(text):
  text1 = list(text)
  print text1
  for i in text1:
    if i.lower() in vowels:
      text1.remove(i)

  text2 = "".join(text1)
  return text2

and here are the tests that I placed:

print anti_vowel("my name is Omar")
print anti_vowel("Hey look Words!")
print anti_vowel("Hey look more Words to look for!")

I tried placing print statements in the middle of the code to test it and I found something weird. the for loop iterates about 3 or 4 times to remove one vowel. I can't seem to know why

Upvotes: 0

Views: 253

Answers (5)

Michael Swartz
Michael Swartz

Reputation: 858

This can be done without remove()

#!python2

def anti_vowel(text):
    vowels = ["a", "e", "i", "o", "u"]
    s1 = ''

    for i in text:
        if i.lower() in vowels:
            pass
        else:
            s1 += i
    return s1

print anti_vowel("my name is Omar")
print anti_vowel("Hey look Words!")
print anti_vowel("Hey look more Words to look for!")

Upvotes: 0

Ancora Imparo
Ancora Imparo

Reputation: 351

I am using Python 2.7. I modified your code slightly as follows,

vowels = 'aeiou'
def anti_vowel(text):
    text1 = list(text)
    print text1
    for i in text:
        if i.lower() in vowels:
            text1.remove(i)

    text2 = "".join(text1)
    return text2

print anti_vowel("my name is Omar")
#['m', 'y', ' ', 'n', 'a', 'm', 'e', ' ', 'i', 's', ' ', 'O', 'm', 'a', 'r']
#my nm s mr

print anti_vowel("Hey look Words!")
#['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'W', 'o', 'r', 'd', 's', '!']
#Hy lk Wrds!

print anti_vowel("Hey look more Words to look for!")
#['H', 'e', 'y', ' ', 'l', 'o', 'o', 'k', ' ', 'm', 'o', 'r', 'e', ' ', 'W', 'o', 'r', 'd', 's', ' ', 't', 'o', ' ', 'l', 'o', 'o', 'k', ' ', 'f', 'o', 'r', '!']
#Hy lk mr Wrds t lk fr!

I cannot duplicate your problem. The for loop sweeps through the input string once (one character by one character) and removes any vowel encountered. Perhaps you can post your output so we can debug.

Upvotes: 0

NekoTony
NekoTony

Reputation: 54

Because the for statement checks through every letter within the list, even when it removes that vowel. Here's a quick example where it doesn't iterates 3-4 times to remove one vowel:

vowels = ["a", "e", "i", "o", "u"]
def anti_vowel(text):
  text1 = list(text.lower())
  text1 = [x for x in text1 if x not in vowels]
  text2 = "".join(text1)
  return (text2)

print anti_vowel("my name is Omar")
print anti_vowel("Hey look Words!")
print anti_vowel("Hey look more Words to look for!")

Upvotes: 0

Levi Lesches
Levi Lesches

Reputation: 1611

This weird case happens when 2 vowels are right next to each other. Basically, you are looping for each letter in the word, but when you remove the letter (if it is a vowel), then you shorten the length of the word, and therefore the next letter will have skipped over the real next letter. This is a problem when the letter being skipped is a vowel, but not when it is a consonant.

So how do we solve this? Well, instead of modifying the thing we're looping over, we will make a new string and modify it. So:

text2 = ""    
for letter in text1:
    if letter not in vowels:
        text2 += letter
return text2

This can also be achieved with list comprehension:

return "".join ([letter for letter in text1 if letter not in vowels])

Upvotes: 2

bharath
bharath

Reputation: 346

You don't have to convert the string to a list. Refer to this answer here:

Correct code to remove the vowels from a string in Python

Upvotes: 0

Related Questions