Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
defaultdict solution in Clear category for Switch Keys to Values by kdim
from collections import defaultdict
def switch_dict(data: dict) -> dict:
res = defaultdict(set)
for k, v in data.items():
res[v].add(k)
return res
print("Example:")
print(switch_dict({"rouses": "red", "car": "red", "sky": "blue"}))
assert switch_dict({"rouses": "red", "car": "red", "sky": "blue"}) == {
"red": {"car", "rouses"},
"blue": {"sky"},
}
assert switch_dict({"1": "one", "2": "two", "3": "one", "4": "two"}) == {
"one": {"3", "1"},
"two": {"4", "2"},
}
assert switch_dict({"a": "b", "b": "c", "c": "a"}) == {
"b": {"a"},
"c": {"b"},
"a": {"c"},
}
print("The mission is done! Click 'Check Solution' to earn rewards!")
Oct. 20, 2022
Comments: