Reputation: 747
For example,
a=[[a, 1], [b, 1], [1, 1]]
I want to find how many "1"s there are, but only those that are the second element in the nested lists. So it should give me 3, ignoring the "1" in the third list as it is the first element in the list.
Upvotes: 3
Views: 301
Reputation: 4963
You might use:-
[item for sub_list in a[1:] for item in sub_list].count(1) # 3
Upvotes: 1
Reputation: 92854
Use collections.Counter subclass to count occurrences of any value:
import collections
a = [['a', 1], ['b', 1], [1, 1]]
counts = collections.Counter((l[1] for l in a))
print(counts[1]) # 3
Upvotes: 3