Kodeclik Blog

Shuffling a Python dictionary

Assume we have a Python dictionary like so:
and we seek to shuffle it. How can we go about it?
First, what do we mean by shuffling a Python dictionary? Note that a dictionary is “unsorted”, i.e., there is no ordering to the elements. All a dictionary is meant to capture is the association (mapping) between keys and values. So shuffling a Python dictionary does not quite make sense in the normal meaning of the word.
There are potentially two meanings to shuffling a Python dictionary. First, shuffling could mean printing its elements in a different, non-default, order. In this case, we should view the dictionary as a list and re-arrange the list elements. Second, it could mean that we keep the keys the same and randomly shuffle its values. In this case, the dictionary stays a dictionary. We will explore programs for both these cases next!

Shuffling 1: Changing the printed order of elements in a dictionary

Here we are going to retain the association between months and days but simply print them in a different order. So we convert the dictionary to a list and simply randomize the order of elements in the list. Here is a program to accomplish our goal:
Note that we are using the Python library random because it contains a shuffle() function that we can use. In this program, first we convert the calendar dictionary into a list called “months”. We print the months list just to show what it looks like. You can see that the months are in order from January to December. Then we do random.shuffle() on the months list. Observe that this function needs a list as input, so we could not have given the original dictionary as an input. Once the months are shuffled, we do our print operation again. The output is:
Thus our purpose is achieved. Note in particular that the association between month names and days in a month is not destroyed. Just the order in which they are printed is changed. If you run this program again, your output will change.

Shuffling 2: Reassigning values to keys in a dictionary

In the second approach, we wish to randomize the assignment of values to keys. The dictionary stays a dictionary. Here is a program for this purpose:
Here the calendar variable is a dictionary. We first convert the keys into a list, shuffle it (recall that shuffling works only with a list). After shuffling we re-pair the shuffled keys with the (unshuffled) values using the zip() function and then convert the resulting pairs of values into a new dictionary, called new_calendar:
Note how February now has 31 days and March is the month that has 28 days.
Python dictionaries provide users with an efficient way of organizing and manipulating data. But sometimes we might need to “shuffle” its entries. We have seen two different interpretations of what shuffling means and how to go about them. Explore these ideas and let us know how you use them in your own applications!
If you liked learning to shuffle dictionaries, learn how to unpack a dictionary into variables. Also see how to sort a Python dictionary by keys!
For more Python content, checkout the math.ceil() and math.floor() functions! Also learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.