This works perfectly
def get_count(sentence):
return sum(1 for letter in sentence if letter in ('aeiou'))
But when I apply the in operator to an array like this, it fails
# Incorrect answer for "aeiou": 0 should equal 5
def get_count(sentence):
return sum(1 for letter in sentence if letter in ['aeiou'])
In the first snippet,
('aeiou')is a string surrounded by parentheses (which have no syntactic function in this case). Theinis applied to the string, which is itself an iterable, and thus checks ifletteris one of the characters in that string.In the second snippet,
['aeoiu']is a list with a single element, the string'aeiou'. Theinoperator applies to the list, and checks ifletteris one of the elements in the list. It obviously isn't (sinceletteris a single character), and thus this condition always evaluates toFalse.