Is this good way to do anti_vowel(8/15)?


#1

def anti_vowel(text):
  s = ""
  vowels = "aeiouAEIOU"
  for i in text:
    if i not in vowels:
      s += str(i)
  return s



#2

str() isn’t needed, given i is of type string

the big problem with this solution, is that strings are immutable, so when you concatenate a string like you do here:

s += str(i)

a new string is created with the combined result (each iteration of the loop)

ideally, you should use a mutable data type (like a list) and then .join() the list into a string, and return it

For the rest, its a good solution :slight_smile:


#3

Is this what ok solution would be?

def anti_vowel(text):
  s = ""
  ss = []
  vowels = "aeiouAEIOU"
  for i in text:
    if i not in vowels:
      ss += i
  print s.join(ss)

#4

i would prefer to use .append() over list extending method (+=), but yes, that is the way to go


#5

Oh yes, .append() would be right way since it is list. Thanks a lot for reply mate!


#6

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