Question: Write a function shuffle _ dict ( somedict ) that behaves as follows. Its argument somedict is a dictionary. This function returns a new dictionary

Write a function shuffle_dict(somedict) that behaves as follows. Its argument somedict is a dictionary. This function returns a new dictionary constructed as follows:
Let sorted_keys be the sorted list of the keys of somedict.
Let sorted_vals be the sorted list of the values that these keys are mapped to in somedict.
Construct a dictionary shuffled_dict that maps sorted_keys[0] to sorted_vals[0], sorted_keys[1] to sorted_vals[1], sorted_keys[2] to sorted_vals[2],... sorted_keys[n] to sorted_vals[n].
Return the dictionary shuffled_dict so constructed.
Note: You can get the set of values in a dictionary somedict using somedict.values(). This is analogous to the keys() method that gives the keys in a dictionary. In each case, you then need to use list() to obtain a list.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!