# Count occurences of number in list of lists

I have a nested list of numbers, something like satisfied = {{1, 5, 6, 2, 1, 2}, {1, 5,,8, 6}, ..., {5, 6, 1}} and I want to count the number of occurences of 1 in each list. I know there is 1000 of them.

I have tried

Do[Count[satisfied[[i]], 1], {i, 1000}]


since this works for one chosen list

Count[satisfied[[1]], 1]

• To work with lists, refer to List Manipulation Nov 12, 2022 at 20:09
• Cases[yourList, 1, Infinity] // Length Nov 13, 2022 at 17:32

## 1 Answer

There is no need to use a loop like this in Mathematica. Use the Count function and just map it. You do not need to know the length of the list either.

lis = {{1, 5, 6, 2, 1, 2}, {1, 5, 8, 6}, {5, 6, 1}}
Count[#, 1] & /@ lis


There are many other ways to do this in Mathematica.

• Thank you. I am new to this Nov 12, 2022 at 20:07
• Very nice. In addition, using the operator form of Count: Count[1]/@lis Nov 12, 2022 at 20:20
• @user1066 that is good also. I myself have not looked at operator forms so I do not know much about it. I should look at these one day. I know there are at least 10 different ways to do the same thing in Mathematica, so it is a matter of finding the best method to use. Nov 12, 2022 at 20:23