Given a dict d whose keys and values are strings, return a new dict with the keys and values swapped: every value of d becomes a key, and it maps to the key it belonged to.
The order of the entries in the dict you return doesn't matter.
Example 1:
Input: d = {"apple": "red", "banana": "yellow", "plum": "purple"}
Output: {"red": "apple", "yellow": "banana", "purple": "plum"}
Explanation: "apple" mapped to "red", so now "red" maps to "apple". The other two entries turn around the same way.
Example 2:
Input: d = {"cat": "dog"}
Output: {"dog": "cat"}
Explanation: One entry gives one entry, turned around.
Example 3:
Input: d = {"a": "b", "b": "c"}
Output: {"b": "a", "c": "b"}
Explanation: "b" is both a key and a value of d. As the value of "a", it becomes a key that maps to "a". As a key, its entry "b": "c" turns into "c": "b".
Rules:
dhas at least one entry.- All the values of
dare different, so the result has as many entries asd. - Keys and values are words without spaces.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
