• list filtering issue

Question related to mission Bird Language


I'm trying using itertools to figure out this useful module but I don't understand why the filterfalse line doesn't return the value I expected. It should remove just a single vowel after a consonant but it doesn't.

  • input: "hoooowe yyyooouuu duoooiiine"
  • the line returns:['h', 'w', ' ', 'y', 'y', 'y', 'u', 'u', 'u', ' ', 'd', 'u', 'i', 'i', 'i', 'n']
  • what I expected:['h', 'o', 'o', 'o', 'w', ' ', 'y', 'y', 'y', 'o', 'o', 'o', 'u', 'u', 'u', ' ', 'd', 'o', 'i', 'i', 'i', 'n']

Could anyone tell me why it leaves out all 'o's and keeps 'u' after 'd'? Thank you.

import itertools
VOWELS = "aeiouy " #"aeiouy" + " "

def translate(data):
    data = list(itertools.filterfalse(lambda x: data[data.index(x)-1] not in VOWELS, data))     
    data = [list(g) for k,g in itertools.groupby(data)]
    data = [i if i[0] not in VOWELS or len(i) < 3 else [i[0]] * (len(i) // 3) for i in data]
    return "".join([e for i in data for e in i])