Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
From {"a": 1, "b": 2, "c": 3}, make a new dictionary that has the values as keys and the keys as values.
A key appears only once, so values must be different if they become keys.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
Plan. Go through the pairs. For each one, store the value as a key and the key as its value.
The result is {1: 'a', 2: 'b', 3: 'c'}.
Be careful. This works only if the values are all different. If two keys had the same value, the later one would replace the earlier one, because a key can appear only once.
Log in to join the discussion.
Loading…