Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Switch Keys to Values by mikehult123
def switch_dict(data: dict[str, str]) -> dict[str, str]:
a = {}
for i, q in data.items():
a[q] = a.setdefault(q, set()) | {i}
return a
print("Example:")
print(switch_dict({"rouses": "red", "car": "red", "sky": "blue"}))
# These "asserts" are used for self-checking
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": {"1", "3"},
"two": {"2", "4"},
}
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!")
Nov. 26, 2024
Comments: