Sobes.tech
Middle+

Task: write a function that reverses a dictionary (swaps keys and values). For example, {1:2, 3:4} → {2:1, 4:3}.

sobes.tech AI

Answer from AI

Here's an example of a Python function that reverses a dictionary by swapping keys and values:

def invert_dict(d):
    return {v: k for k, v in d.items()}

# Example usage
original = {1: 2, 3: 4}
inverted = invert_dict(original)
print(inverted)  # Will output: {2: 1, 4: 3}

It's important to note that the values of the original dictionary should be unique and hashable to be used as keys in the new dictionary.

Task: write a function that reverses a dictionary… - sobes.tech