-6

how can I count '2' in this dict?

{'count': 2, 0: '1', 1: '2'}

Is there any built-in function?

Nima Ra
  • 25
  • 9
  • 5
    I don't know if it matters to your question, but that's a dictionary, not a list. Generally for counting stuff you would use a [`Counter`](https://docs.python.org/3/library/collections.html#collections.Counter), but your question doesn't really make it clear what it is you're trying to count – Patrick Haugh Oct 31 '17 at 17:38
  • 1
    This question is very hard to understand as written. Please refer to our [help center](https://stackoverflow.com/help) for help writing questions – bendl Oct 31 '17 at 17:44

1 Answers1

0

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())
user2390182
  • 72,016
  • 6
  • 67
  • 89