3 examples of 'python count keys in dict' in Python

Every line of 'python count keys in dict' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
29def count_value(lst, key):
30 d = {}
31 for obj in lst:
32 val = obj[key]
33 if val in d:
34 d[val] = d[val] + 1
35 else:
36 d[val] = 1
37 return d.iteritems()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

4def increment_count(count_dict, key, n=1):
5 """
6 Puts the key in the dictionary if does not exist or adds one if it does.
7 Args:
8 count_dict: a dictionary mapping a string to an integer
9 key: a string
10 """
11 if key in count_dict:
12 count_dict[key] += n
13 else:
14 count_dict[key] = n
14def iteritems(d):
15 return d.iteritems()

Related snippets