How to Count Co-occurrence Pairs in Python with Nested Dictionaries
This code counts how often any two items appear together in the same group, using a nested defaultdict keyed by item pairs.
Python code
19 linesfrom itertools import combinations
from collections import defaultdict
def count_cooccurrences(items_per_group):
cooccurrence = defaultdict(lambda: defaultdict(int))
for group in items_per_group:
for a, b in combinations(sorted(group), 2):
cooccurrence[a][b] += 1
cooccurrence[b][a] += 1
return {k: dict(v) for k, v in cooccurrence.items()}
if __name__ == "__main__":
transactions = [
["milk", "bread", "eggs"],
["bread", "butter"],
["milk", "bread", "butter"]
]
result = count_cooccurrences(transactions)
print(result)
Output
{'milk': {'bread': 2, 'eggs': 1, 'butter': 1}, 'bread': {'milk': 2, 'eggs': 1, 'butter': 2}, 'eggs': {'milk': 1, 'bread': 1}, 'butter': {'bread': 2, 'milk': 1}}
How it works
defaultdict(lambda: defaultdict(int)) builds a two-level dictionary where missing keys default to an int counter, so increments never raise a KeyError. Sorting each group before combinations ensures that only one order of a pair is considered, yet the code still records both directions for easy lookups. The outer defaultdict is converted to a normal dict at the end for cleaner printing.
Common mistakes
- Forgetting to sort the group before combinations, leading to duplicate pairs counted inconsistently
- Using a single-level dict and manually checking keys, missing pairs when one item appears in many contexts
Variations
- Use a dict of dicts with setdefault(key, {}).setdefault(pair, 0) instead of defaultdict.
- Combine with pandas.crosstab when working on larger datasets with pandas.
Real-world use cases
- Building a recommendation engine by counting items bought together from purchase transactions.
- Analyzing word co-occurrences in text for topic modeling or NLP feature extraction.
- Detecting frequent patterns in log events to identify correlated failures in a system.
Sponsored
More from Dictionaries & sets
- Build a Case-Insensitive Dict with a Wrapper Class in Python medium
- Build a defaultdict histogram of categories in Python easy
- Build adjacency dict graph from edges in Python easy
- Build an OrderedDict insertion order demo in Python 3 easy
- Check Invertible Mapping for Duplicate Values in Python easy
- Compare Two Dictionaries in Python easy
Keep learning
Related tutorials and quizzes for this topic.