How do I split dicts in Python in pure Python?

In Python, splitting dictionaries can be useful when you want to separate data based on specific conditions or keys. Here’s a simple way to do it using pure Python:

def split_dict(original_dict, condition): true_dict = {} false_dict = {} for key, value in original_dict.items(): if condition(key, value): true_dict[key] = value else: false_dict[key] = value return true_dict, false_dict # Example usage original = {'a': 1, 'b': 2, 'c': 3, 'd': 4} condition = lambda k, v: v > 2 true_dict, false_dict = split_dict(original, condition) print("True dict:", true_dict) # {'c': 3, 'd': 4} print("False dict:", false_dict) # {'a': 1, 'b': 2}

Python Dictionary Split Dictionary Key-Value Pairs Condition