Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

Kodeclik Blog

Shuffling a Python dictionary

Assume we have a Python dictionary like so:

calendar = {'January': 31,
            'February': 28,
            'March': 31,
            'April': 30,
            'May': 31,
            'June': 30,
            'July': 31,
            'August': 31,
            'September': 30,
            'October': 31,
            'November': 30,
            'December': 31}

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.

Python shuffle dictionary

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:

import random

calendar = {'January': 31,
            'February': 28,
            'March': 31,
            'April': 30,
            'May': 31,
            'June': 30,
            'July': 31,
            'August': 31,
            'September': 30,
            'October': 31,
            'November': 30,
            'December': 31}

months = list(calendar.keys())
print([(m, calendar[m]) for m in months])

random.shuffle(months)
print([(m, calendar[m]) for m in months])

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:

[('January', 31), ('February', 28), ('March', 31), ('April', 30), 
('May', 31), ('June', 30), ('July', 31), ('August', 31), 
('September', 30), ('October', 31), ('November', 30), ('December', 31)]
[('October', 31), ('March', 31), ('February', 28), ('November', 30), 
('July', 31), ('January', 31), ('June', 30), ('December', 31), 
('September', 30), ('May', 31), ('April', 30), ('August', 31)]

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:

import random

calendar = {'January': 31,
            'February': 28,
            'March': 31,
            'April': 30,
            'May': 31,
            'June': 30,
            'July': 31,
            'August': 31,
            'September': 30,
            'October': 31,
            'November': 30,
            'December': 31}

print(calendar)

months = list(calendar.keys())
random.shuffle(months)

new_calendar = dict(zip(months, calendar.values()))
print(new_calendar)

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:

{'January': 31, 'February': 28, 'March': 31, 'April': 30, 'May': 31, 
'June': 30, 'July': 31, 'August': 31, 'September': 30, 'October': 31, 
'November': 30, 'December': 31}
{'November': 31, 'March': 28, 'October': 31, 'April': 30, 'September': 31, 
'December': 30, 'January': 31, 'July': 31, 'May': 30, 'June': 31, 
'August': 30, 'February': 31}

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.

Kodeclik sidebar newsletter

Join our mailing list

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

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.