Using your own code you need to filter characters not in vowels, you can lower each character and make vowels a set for O(1) lookups:
def numVowels(s):
vowels = {"a","e","i","o","u"}
if s == '':
return 0
else:
totalVowels = list(filter(lambda x: x.lower() in vowels , s))
return len(totalVowels)
Which can simply become:
def numVowels(s):
vowels = {"a","e","i","o","u"}
return len(list(filter(lambda x: x.lower() in vowels , s)))
Each x in the lambda is each character from s, passing lambda x: x.lower() in vowels to filter will filter any character from s that is not in our vowels set so we are left with a list of just non-vowles.
You can combine sum with your filter and lambda:
def numVowels(s):
vowels= {"a","e","i","o","u"}
return sum(1 for _ in filter(lambda x: x.lower() in vowels , s))
Or generally when a lambda was not a nesessity, simply returning the generator expression would be the best approach:
def numVowels(s):
vowels= {"a","e","i","o","u"}
return sum(x.lower() in vowels for x in s))
The last part of the answer is the most efficient for large input as sets are O(1) for lookups. x.lower() in vowels will either be True/False 1 or 0 so you just need to sum them all. If s is an empty string or there are no vowels sum will return 0
An example of how x.lower() in vowels works:
In [8]: tests = "String with vowels"
In [9]: l = [x.lower() in vowels for x in tests]
In [10]: l
Out[10]: # list of booleans
[False,
False,
False,
True,
False,
False,
False,
False,
True,
False,
False,
False,
False,
True,
False,
True,
False,
False]
In [11]: l = [int(b) for b in l]
In [12]: l # list of 1's and 0's equating to True and False
Out[12]: [0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0]
lambda vowels:sum("AEIOUaeiou")doessum()the vowels, which makes no sense.