Skip to content Skip to sidebar Skip to footer

Def Anti_vowel - Codecademy (python)

I am a bit stuck on creating the anti_vowel definition: Define a function called anti_vowel that takes one string, text, as input and returns the text with all of the vowels remov

Solution 1:

Consider:

>>>tgt='This is some text with vowels'>>>vowels='aeiou'>>>''.join(e for e in tgt if e.lower() notin vowels)
'Ths s sm txt wth vwls'

Or, as pointed out in comments, using an actual list comprehension inside join is better:

>>>''.join([e for e in tgt if e.lower() notin vowels])
'Ths s sm txt wth vwls'

Solution 2:

return''.join(av)

will make it back into a string from a list.

Solution 3:

You're returning the list. Using join function you can convert the list to corresponding string.

Try:

return''.join(av)

instead of return av

Solution 4:

You're looking for str.join which takes an iterable (like a list) as an argument and returns the string formed by using the object as a separator between each element of that iterable. For example:

lst = ['a','b','c','d','e','f','g']

''.join(lst) # 'abcdefg''/'.join(lst) # 'a/b/c/d/e/f/g''a'.join(lst # 'aabacadaeafag' etc etc...

Note that there are better ways to implement your function. str.translate(use THIS str.translate in Python2, which iirc is what Codecademy uses for their interpreter, it's changed between versions) is BORN to delete characters from strings.

# Python3defhate_vowels_py3(instring):
    transmap = str.maketrans('', '', 'aeiouAEIOU')
    outstring = instring.translate(transmap)
    return outstring

# Python2defhate_vowels_py2(instring):
    outstring = instring.translate(None, 'aeiouAEIOU')
    return outstring

And using your method, there's even an incremental improvement to be made. Any time you both

  • Don't need to keep something in order and
  • Don't need to have more than one of something

You should strongly consider using a set instead of a list. The set takes slightly longer to build for the first time, but lookups are MUCH faster. Remember that if you unroll that list comp you have:

# example string"Hello world!"

chars_to_delete = ["a","e","i","o","u","A","E","I","O","U"]
string = "Hello world!"
output_list = []
char = "H"
can_append_to_list = Trueifchar == "a": can_append_to_list = Falseifchar == "e": can_append_to_list = Falseifchar == "i": can_append_to_list = Falseifchar == "o": can_append_to_list = Falseifchar == "u": can_append_to_list = Falseifchar == "A": can_append_to_list = Falseifchar == "E": can_append_to_list = Falseifchar == "I": can_append_to_list = Falseifchar == "O": can_append_to_list = Falseifchar == "U": can_append_to_list = False # ten lookups to this list
if can_append_to_list: output_list.append(char)
char = "e"
can_append_to_list = True
# begin again..........

As you can see that's a LOT of lookups to that list. Instead use a set, whose lookup time is BLINDINGLY fast. Either use the literal declaration:

vowels = {'a','e','i','o','u','A','E','I','O','U'} # looks like a dict with only keys# (also IS a dict with no keys, but shhh that's an implementation detail)

or you can create one from any iterable by calling set() with the iter as an argument

vowels = set('aeiouAEIOU')

Solution 5:

This is my solution:

def anti_vowel(text):
    vowels = 'aeiouAEIOU'
    textlist = []
    forcharintext:
        ifcharin vowels:
            continueelse: 
            textlist.append(char)      
    return"".join(textlist)

Post a Comment for "Def Anti_vowel - Codecademy (python)"