how can I count '2' in this dict?
{'count': 2, 0: '1', 1: '2'}
Is there any built-in function?
how can I count '2' in this dict?
{'count': 2, 0: '1', 1: '2'}
Is there any built-in function?
First, it is a dict
, not a list
. You can use sum
with a generator:
d = {'count': 2, 0: '1', 1: '2'}
s = sum(v == 2 for v in d.values())
If you want to catch both 2
(the integer) and '2'
(the string):
s = sum(v in ('2', 2) for v in d.values())