Why Does This Code Fail if There Are Two or More Consecutive Vowels?


#1

my code fails if two or more vowels come consecutive.(and pls check if there are other fails to my code)my code is:
def anti_vowel(text):
b=list(text)
a=[‘a’,‘e’,‘i’,‘o’,‘u’,‘A’,‘E’,‘I’,‘O’,‘U’]
for w in b:
if w in a:
b.remove(w)
c=’’.join(b)
print c
anti_vowel(“Hey You!”)


FAQ: Learn Python - Practice Makes Perfect - anti_vowel
#2

the other failure would be that your function doesn’t return the string without vowels

.remove() is a very difficult approach.

lets say we want to remove all the vowels from look, then look has the following indexes:

l   o   o   k
0   1   2   3

the problem is that when the letter o gets removed from index 1, that space can’t stay empty. So python is going to shift everything to the right of the removed item one space to the left, so then we get:

l   o   k
0   1   2

which is a problem, because the loop is going to the next index (2), causing the remaining o to be skipped

now of course there are ways to overcome this problem, but the moment you have to workaround a problem which is caused by your solution, you should first question if there aren’t better solutions


#3

what if i immediately append empty string like ‘’


#4

it might be possible, but you would have to keep track of indexes/indices as well, but i would advise against it

^ like i said, if you have to implement workarounds, question if your solution is the right one in the first place


#5

ya i think i’m stuck may be this isn’t the right approach.


#6

it isn’t the right approach, you can remove maybe from that sentence. What guidance do you want from me towards working on a different solution? I do you prefer going back to the drawing board on your own?


#7

OK may be I’ll try it again by my own.but thanks for your time.if there is something you say as a hint you can give me.


#8

but the hint could be too big, here is the hint blurred in spoilers:

you could append the consonants to a new list or string.

you can click the blur to make it visible.


#9

I didn’t run into the issue of a skip occurring from consecutive vowels using .remove() in this code.

def anti_vowel(text):
  result = list(text)
  for v in text:
    if v in "aeiouAEIOU":
      result.remove(v)
  return "".join(result)
   
print anti_vowel("The quick brown fox jumps over the lazy dog.")
#Th qck brwn fx jmps vr th lzy dg.

So I decided to give @byterunner45723 code a try, and see where we differ. I noticed in the for-loop they’re iterating through the variable b, instead of the function argument text. which leads me to believe it isn’t a problem regarding the foundation of .remove(), but how it’s being implemented. I would chalk it up to human error. However, what you said makes a lot of sense, referring to the code in it’s original state. I didn’t consider the shifting indices when I was first looking at it.

I modified their code a bit and everything ran accordingly. This is what I came up with,

def anti_vowel(text):
  b=list(text)
  a=['a','e','i','o','u','A','E','I','O','U']  
  for w in text:
    if w in a:
      b.remove(w)
  c=''.join(b)
  return c

print anti_vowel("The quick brown fox jumps over the lazy dog.")
#Th qck brwn fx jmps vr th lzy dg.

Anything you can add to this is greatly appreciated. I’m trying to learn as much as I can. :smile:


#10

its because you are not looping over the same list as you are removing from, then everything goes fine.

byterunner is looping over and removing from the same list (which he named b)

if we do:

def anti_vowel(text):
  b=list(text)
  a=['a','e','i','o','u','A','E','I','O','U']  
  for w in b:
    if w in a:
      b.remove(w)
  c=''.join(b)
  return c

print anti_vowel("The quick brown fox jumps over the lazy dog.")

you should have the mistake.

and yes, its possible to loop over b, which is just a list copy of the text string (parameter)