Skip to content

Instantly share code, notes, and snippets.

@1Mark
1Mark / itertools_combinations_dict.py
Last active August 22, 2021 15:59
How to get all subsets of a dictionary in python?
from itertools import combinations
from typing import List
def subsets(the_dict: dict) -> List[dict]:
subsets_as_tuples = []
dict_as_tuple = the_dict.items()
# We start from 1 in the range since list(combinations(some_dict, 0)) returns an empty list
for i in range(1, len(the_dict) + 1):
subsets_as_tuples.extend(list(combinations(dict_as_tuple, i)))
print(subsets_as_tuples)
@1Mark
1Mark / gist:c65bd3ab63cc457fbd65a2848e490863
Last active December 20, 2020 16:35
defaultdict_employees_with_highest_salary
python -m timeit -s "from defaultdict_salary_technique import highest_paid_employees" "highest_paid_employees()"
100000 loops, best of 5: 2.5 usec per loop
python -m memory_profiler defaultdict_salary_technique.py
Filename: defaultdict_salary_technique.py
Line # Mem usage Increment Occurences Line Contents
============================================================
10 37.953 MiB 37.953 MiB 1 @profile