Situatie
Funcția filter() extrage elemente dintr-un iterabil (listă, tuplu etc.) pentru care o funcție returnează True.
Solutie
Pasi de urmat
letters = [‘a’, ‘b’, ‘d’, ‘e’, ‘i’, ‘j’, ‘o’]
# a function that returns True if letter is vowel
def filter_vowels(letter):
vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
return True if letter in vowels else False
filtered_vowels = filter(filter_vowels, letters)
# converting to tuple
vowels = tuple(filtered_vowels)
print(vowels)
Output:
(‘a’, ‘e’, ‘i’, ‘o’)
Leave A Comment?