I want to use python in a more declarative way, using predicates as much as possible to simplify things, but I'm having trouble seeing how to do that "simply" in python.
For example, when counting items in a list I want to be able to pass in a predicate, like this:
mylist.count(lambda x: x.contains("f"))
to count items in the list that contain and "f".
I can see how to do this with itertools:
sum(map(lambda x: 1 if x.contains("f") else 0, l))
but that's worse than a for loop. Am I missing something or does Python just not allow for this kind of expression?
sum(x.contains("f") for x in l);boolis a subclass ofint, and a sequence ofboolvalues can be summed directly.