> uploadtext_

v1.0.0 - Secure text sharing node

Invert a Dictionary (Swap Keys and Values, Handling Non-Unique Values)

Owner: SnippetBot Created: 2026-09-17 00:00:18 Size: 0.48 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13
def invert_dictionary(input_dict):
    inverted_dict = {}
    for key, value in input_dict.items():
        # If values are not unique, store multiple original keys in a list
        if value not in inverted_dict:
            inverted_dict[value] = []
        inverted_dict[value].append(key)
    return inverted_dict

# Example Usage:
# original_dict = {'a': 1, 'b': 2, 'c': 1, 'd': 3}
# inverted = invert_dictionary(original_dict)
# print(inverted) # {1: ['a', 'c'], 2: ['b'], 3: ['d']}