Anti_vowel


#1

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096#

Oops, try again. Your code looks a bit off–it threw a “list.remove(x): x not in list” error. Check the Hint if you need help!

Not sure why it brings up this error… please help!

def anti_vowel(text):
    listed = list(text)
    for vowel in "aAeEiIoOuU":
        listed.remove(vowel)
    return listed

#2

Eg.

>>> m = list('abcdefghijklmnopqrstuvw')
>>> for vowel in "aAeEiIoOuU":
	m.remove(vowel)

	
Traceback (most recent call last):
  File "<pyshell#38>", line 2, in <module>
    m.remove(vowel)
ValueError: list.remove(x): x not in list
>>> vowel
'A'
>>> 

If we give list.remove() a value that is not in the list, it will throw this error.

Suggest try another approach that does not use list.remove(). You should know that .remove only takes the first match out of the list. What if there are multiple instances of the same vowel? See below for possible work around.

This can be solved without using any built in methods. See if you can come up with at least one, if not more than one. It is, after all, our chance to practice and explore. There is little to be learned from using built-ins if we are still weak in basic algorithm thinking.

>>> m = list('abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba')
>>> for vowel in "aAeEiIoOuU":
	while vowel in m:
		m.remove(vowel)

		
>>> ''.join(m)
'bcdfghjklmnpqrstvwxyzyxwvtsrqpnmlkjhgfdcb'
>>> 

Notice we use the while condition as a sort of if statement. The list.remove() method is only called when we know the item is in the list. To deal with duplicates we keep the while loop going until all instances are removed. Still, I strongly suggest setting this method aside and find others that are more algorithmic and do not use built-in helper functions.


#3

Came up with this:

def anti_vowel(text):
listed = list(text)
final = ''
for vowel in “aAeEiIoOuU”:
while vowel in listed:
listed.remove(vowel)
return ‘’.join(listed)

What are some of the “other ways” you were talking about to solve this? I’m curious.


#4

Did you really come up with that solution. It looks like it just copies mine. What is the purpose of final = ''?

Try using not in and see if you don’t come up with a working solution. No built-ins.


#5

I didn’t mean “came up with” as in I made it up. It’s just how I completed the exercise. Thank you for the help.

Final = ‘’ is something I had thought I would need but didn’t


#6

Completing the lesson is not as important as taking the time to explore other options. There is nothing to be gained if once you walk away from here you cannot come up with solutions on your own. At this point in the learning curve we are not seeking nor it is expected that we will come up with cool and elegant solutions. Ugly is fine, so long as we are exploring.

What did you come up with that uses not in?


#7

def anti_vowel(text):
listed = list(text)
final = ''
for vowel not in ‘aAeEiIoOuU’:
while vowel in listed:
final.append(vowel)
return final

But it didn’t work:
File “python”, line 4
for vowel not in ‘aAeEiIoOuU’:
^
SyntaxError: invalid syntax

The “carrot” points to the “not” in the error screen


#8

str objects do not have an append method, only list objects have that. Make final into an empty list. You will not need a while loop for this solution. Also, you will not be iterating over the vowels, but the actual old list (can be a string).

    for i in text:

That would be, carat, which does not necessarily point to the actual error, only the point at which parsing stopped because of something unexpected.

Keep working at it.


#9

Took your suggestion and tried this:
def anti_vowel(text):
listed = list(text)
final = []
for vowel not in ‘aAeEiIoOuU’:
while vowel in listed:
final.append(vowel)
return str(final)
Same error came up. Trying to figure out why.

Is “not in” even a thing I can do, or will I have to find some way around that?


#10

Yes it is, but not in a for loop.


#11

Tried this:

def anti_vowel(text):
listed = list(text)
final = []
for vowel in listed:
while vowel not in ‘aAeEiIoOuU’:
final.append(vowel)
return str(final)

Didn’t work. I really don’t know how to use “not in” in this circumstance

Oops, try again. Your code looks a bit off–it threw a “” error. Check the Hint if you need help!


#12

You have used it correctly (almost). Remember we said there would be no while loop? I would choose a different variable name, though.

for letter in text:

You don’t need to convert this to a list since all we’re doing is iterating over it.

    if letter not in 'aeiouAEIOU':

You are appending correctly but the only flaw is the use of the str() function in your return. It will turn a list into a string, just not what we expect…

str([1,2,3,4,5])  =>  '[1,2,3,4,5]'

Use the join method to convert a list to a string.

''.join(final)

When you get this working, see if your can perform the entire operation using only strings and no lists. Again, no built-ins.


#13

Tried it, worked. Very helpful. Thank you!


#14

Did you try a string only solution. or are you tired of this exercise and want to move on? When you come back to review, keep this challenge in mind.


#15

Hi, I was working on the same exercise, and came across many of the same problems. I even tried to “not in” exercise, but encountered the same problem.
You said, when referring to the not in variation:

Blockquote You don’t need to convert this to a list since all we’re doing is iterating over it.

What are we doing differently in the original solution that meant we had to turn it into a list? And why won’t the not in function we are trying to use work in a while loop, but it will work in a for loop?

Thank you!


#16

The original proposed solution included the use of a list method, so we needed a list to expose this method. Under normal conditions working with the strings is perfectly adequate.

    no_vowels = ''

    for i in text:
        if i not in 'aeiouAEIOU':
            no_vowels += i

It won’t work in the for loop since it is a boolean…

for x not in y:

translates to,

for True:  # or for False:

which is invalid.

The while loop however works directly with a boolean…

while x not in y:

Assuming x has some value, and y is a reference object or string, this loop will continue until x is set to something that IS in y.

Again, it went back to working with the built in, which while a useful and elegant solution, is not very algorithmic as the built-in does all the heavy lifting. We need to think through basic algorithms that use a minimum of available tools. This helps us build upon our basic knowledge and skill set. Vitally important moving forward as it teaches us how to think in a step by step approach.


#17

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.