Reverse Lookup
Sometimes you need to build a reverse lookup table, so you can search by the old value instead of the old key. Write a function invert_dict that receives a dictionary with string keys and unique string values. Return a new dictionary where every key and value have swapped places.
Примеры
Ввод
{"a": "x", "b": "y"}Вывод
{"x": "a", "y": "b"}Ввод
{}Вывод
{}Ввод
{"x": "y"}Вывод
{"y": "x"}Нужно реализовать
def invert_dict(d: dict) -> dict:
Теги
dicthashmap
Zaloguj się, aby zobaczyć historię rozwiązań
Zaloguj sięZaloguj się, aby korzystać z Mentora AI
Zaloguj sięVisible tests
1
Input
{"a":"x","b":"y"}Expected
{"x":"a","y":"b"}2
Input
{}Expected
{}3
Input
{"x":"y"}Expected
{"y":"x"}4
Input
{"one":"1","two":"2","three":"3"}Expected
{"1":"one","2":"two","3":"three"}5
Input
{"k":"v"}Expected
{"v":"k"}6
Input
{"a":"b","c":"d"}Expected
{"b":"a","d":"c"}Focus radio
Paused · SomaFM · Fluid