| title | Key-Value Transformation in a Dictionary | ||
|---|---|---|---|
| tags |
|
Write a function reverse_dict(d: dict) -> dict that reverses the keys and values of a dictionary. Assume values are unique and hashable. Sample Input
{1: 'a', 2: 'b', 3: 'c'}
Sample Output
{'a': 1, 'b': 2, 'c': 3}
<prefix>
def reverse_dict(d: dict) -> dict:
"""
Reverses the keys and values of the given dictionary.
Arguments:
d: dict - a dictionary where the values are unique and hashable.
Returns:
dict - a new dictionary with the keys and values reversed.
"""
</prefix>
<template>
return <sol>{value: key for key, value in d.items()}</sol>
</template>{1: 'a', 2: 'b', 3: 'c'}
{'a': 1, 'b': 2, 'c': 3}
{'x': 10, 'y': 20, 'z': 30}
{10: 'x', 20: 'y', 30: 'z'}
{100: 'apple', 200: 'banana'}
{'apple': 100, 'banana': 200}
{'cat': 1, 'dog': 2, 'mouse': 3}
{1: 'cat', 2: 'dog', 3: 'mouse'}