# how to count or extract Cases out of list of association that matches pattern

I have list of associations

lr1 = {<|a -> {0,0}, b -> "TRUE"|>, <|b -> "FALSE", a -> {0,1}|>, <|a -> {1,0}, b -> "FALSE"|>, <|b -> "TRUE", a -> {1,1}|>}


and I would like to count number of associations in a list lr1 that matches pattern that key b is associated with value "TRUE".

Similarly, to extract new list of associations, with only those that matches the sam pattern as in case of counting.

Keys a and b does not necessary have unique positions through associations.

I need to solve this with Mathematica functions Count and Cases.

• "I need to solve this with Mathematica functions Count and Cases." - why the restriction, if other approaches can be more compact or efficient? Nov 12, 2017 at 10:07
• @J.M. - the list of associations is few x 10^6, so primary function is to count acceptable assoc. Next step is extraction. I would avoid making any operations before counting. So, this is reason for counting first. But if this is possible, without generating new datalist please post it. Nov 12, 2017 at 10:19
• So, would Length @* Select[MatchQ["TRUE"] @* Lookup[b]] @ lr1 fit the bill? Nov 12, 2017 at 10:20
• That's the infix form of Composition[]. Nov 12, 2017 at 11:17
• Count[Lookup[lr1, b], "TRUE"] or Count[lr1, "TRUE", {0, ∞}] Nov 12, 2017 at 14:53

This is a good use case for KeyValuePattern:
Count[KeyValuePattern[b -> "TRUE"]] @ lr1