anti_vowel


#1


https://codecademy.com/courses/python-intermediate-en-rCQKw/1/2

Need some help here.

Can't understand why "O" is still in the list.

Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "Hy lk Words!" when it should return "Hy lk Wrds!".


def anti_vowel(text):
    l = list(text)
    for i in l:
        if i in "aeiouAEIOU":
            l.remove(i)
    print "".join(l)
    return "".join(l)

anti_vowel("Hey look Words!")


#2

remove is a rather difficult method to solve method to solve the problem. The problem you are dealing with is index shift, lets look at at hey:

h   e   y
0   1   2

i indexed the letters. Now i am going to loop over them. first at index 0 is h, no vowel so moving to index 1, on index 1 is the letter e is a vowel, so we remove it. However, this causes a shift in index. y will now shift to index 1. The loop now move on to index 2. Which empty (due to the shift). Now, if you where two vowels in a row (like the word look), this shift causes massive problems, the loop is moving on to the next index, while the second o shifts.

I recommend to create a new list, append constants (and the spaces) the new list, and then join it in the end


#3

I just amended the logic a bit. you are both helpful! thanks!


#4

Good to hear you resolved it, two suggestions to improve it:
this line:

l = list(text)

you can remove it, you never alter the list, just append (copy) letters to a new list, do change your loop in that case:

for i in text:

secondly, you can remove this line:

print ''.join(n)

and call print on the function call:

print anti_vowel("Let's remove all the vowels!")

this way, the string doesn't need to be joined twice


#6

why not simple insert a not keyword in your condition?

if c not in "aeiouAEIOU":
    result.append(c)

Apart from that, posting answers without explanation is not according to the guidelines, thank you for your understanding :slight_smile:


#7

yes, i did both "in" and "not in"..both worked.

my bad, this actually my first time looking through forum discussions and replying to a code. my bad, i will take note of that next time


#8

Both work indeed :slight_smile:

No problem, just a heads up. For the rest, we can always use more people helping out on the forum :slight_smile:


#9

what is this actually doing??

return ''.join(n)


#10

it joins the list into a string, tutorial points has some information


#11

thank you very much i was unsure if that was taking the list and changing it over to a string at it joined the list together. much appreciated!!


#12

Hey, I am confused why the below piece of code does not work, it looks fine to me. Can you please me out, i am saturated thinking about the issue .

def anti_vowel(text):
vowel = "aeiouAEIOU"
result = []
for char in range(0, len(text)):
if char in vowel:
result = result.append("")
result = result.append(char)
return result

print anti_vowel("Winter is coming!!")


#14

The phrase, result.append("") is a verb (an action) that makes an assignment to result so we don't need to write, result = ...

        if char in vowel:
            result.append('')
        else:
            result.append(char)

Because we have constructed a list and the expected return value is a string, we need to return the joined list:

return ' '.join(result)

#15

@shivani100, did you solve the problem/exercise? I like the winter is coming, just a few more weeks :slight_smile:


#16

hey, yes i was able to solve the problem :slight_smile: And yes, just few more weeks to go.. So excited :smiley:


#17

really smart, simple and direct!


#18