Another anti_vowel 8/15


#1

Here is my code:

def anti_vowel(text):
  word = []
  for i in text:
    word.append(i)
  return word
  for c in 'aeiouAEIOU':
    word.remove(c)
  return word

but the output is just the same string that I give it (in list form, which I can account for once I get the code right). What do I need to change?


#2

If you want to return a string, then you will need to join the list with an empty string as separator in the final return statement. The first return statement should be removed. That’s the one you are seeing as output.

Is it necessary to build a list from the string? Could we not utilize the built in list() function to convert it?

Be sure your test phrase contains lots of duplicate vowels seeded throughout. That will give you something else to work out. Notice that .remove() only takes out the first one it encounters? How will you make it greedy (global)?

Example

anti_vowel with remove

You will come up against three powerful concepts towards the end of this track…

filter  =>  one of Python's iterators
lambda  =>  the anonymous function

and ternary expressions which are very handy if-else constructs that save a lot of coding when they make sense.

In addition we have default parameters to take into account. They give us added horsepower.

def filtre(text, flag=False, case='aeiouAEIOU'):
  return ''.join(filter(lambda c: c in case if flag else c not in case, text))

For now, stick to what you are learning. This will come to pass, but now is probably not the time to press it. We’re only giving you a sneak peek.


#3

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