2

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 removed

This is my attempt:

def anti_vowel(text):
    vowels=["a","A","e","E","i","I","o","O","u","U"]
    text_input=[""]
    for char in text:
        text_input.append(char)
    av = [char for char in text_input if char not in vowels]
    return av

My code returns the input as separate characters.

This is the error I get:

Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "['', 'H', 'y', ' ', 'l', 'k', ' ', 'W', 'r', 'd', 's', '!']" when it should return "Hy lk Wrds!". 

Could someone please point me in the right direction?

durron597
  • 31,968
  • 17
  • 99
  • 158
DJM91
  • 23
  • 1
  • 4

7 Answers7

3

Consider:

>>> tgt='This is some text with vowels'
>>> vowels='aeiou'
>>> ''.join(e for e in tgt if e.lower() not in 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() not in vowels])
'Ths s sm txt wth vwls'
Community
  • 1
  • 1
dawg
  • 98,345
  • 23
  • 131
  • 206
  • 1
    [use list comps with str.join](http://stackoverflow.com/questions/9060653/list-comprehension-without-python/9061024#9061024) – Adam Smith Jul 30 '14 at 04:50
  • 1
    Well the speed difference there is not *obvious better* but it is *Raymond* so change made ;-) – dawg Jul 30 '14 at 13:09
2
return ''.join(av)

will make it back into a string from a list.

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

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.

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

# Python2
def hate_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 = True
if char == "a": can_append_to_list = False
if char == "e": can_append_to_list = False
if char == "i": can_append_to_list = False
if char == "o": can_append_to_list = False
if char == "u": can_append_to_list = False
if char == "A": can_append_to_list = False
if char == "E": can_append_to_list = False
if char == "I": can_append_to_list = False
if char == "O": can_append_to_list = False
if char == "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')
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

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

Try:

return ''.join(av)

instead of return av

Pankaj Sharma
  • 669
  • 5
  • 12
0

This is my solution:

def anti_vowel(text):
    vowels = 'aeiouAEIOU'
    textlist = []
    for char in text:
        if char in vowels:
            continue
        else: 
            textlist.append(char)      
    return "".join(textlist)
0

another simple way to solve this problem...

def anti_vowel (text):
   new_text=""
   for char in text:
     if char not in "aeiouAEIOU":
       new_text+=char
   return new_text
-1
def anti_vowel(text):
    vowels = "aeiouAEIOU"
    for x in vowels:
        text = text.replace(x, "")
    return text
josliber
  • 43,891
  • 12
  • 98
  • 133
Rahul
  • 1
  • Please answer the question -- "_Could someone please point me in the right direction to improve my code_" -- rather than posting code that also works. – Nic Jun 10 '15 at 15:58