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 Ivan_Malashenko
def switch_dict(data: dict[str, str]) -> dict[str, str]:
d = {}
for k, v in data.items():
d.setdefault(v, set()).add(k)
return d
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": {"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!")
April 14, 2023
Comments: