Is Khristmas in Your Dictionary?

On how to use Python dictionaries

So, we’ve talked a little about tuples (which use parentheses) and lists (which use square brackets). Now let’s talk dictionaries, which use those cute little curly brackets — that is {} — that come standard on keyboard but that you’ve probably never used unless you happen to be a mathematician.

Here’s your chance to put those curly brackets (sometimes called braces) to work. In Monty Python’s Bookshop Sketch, a customer comes in looking for various books, including “A Khristman Karol” and “Knickerless Knickleby.” Alas, the peeved Proprietor has never heard of them, at least not with those spellings.

But, with Python, we can easily enough create a dictionary in which those titles actually exist. Python’s dictionaries represent a kind of baroque list using the curly braces. These fancy lists are totally mutable and typically jam-packed with pairs of stuff called “items.” An item includes a key and a corresponding value or, to put it in plainer English, a name and some data you want to store along with the name.

So, let’s build a quick list based on the Bookshop Sketch. These are titles of books followed by the names of their authors:

Monty_Python_Library = {'Thirty Days in the Samarkind Desert \
with the Duchess of Kent': 'A. E. J. Eliott', 'David \
Coperfield': 'Edmund Wells','Knickerless Knickleby': \
'Edmund Wells', 'A Khristman Karol': 'Edmund Wells', \
'Rarnaby Budge': 'Charles Dikkens', 'Karnaby Fudge' : \
'Darles Chickens', "Olsen's Standard Book of British Birds: \
 Expurgated Version" : 'Olsen','Ethel the Aardvark goes \
Quantity Surveying' : 'Anonymous'}

Each book and its associated author is placed in a string, and a colon is used to connect them. If you don’t want to do all that typing, you can paste that into your text editor.

Note that I’ve added backslashes (\) to the code so that it can be broken into multiple lines and still run. Those backslashes are not part of the dictionary code. If you remove all the backslashes but put all the code onto a single line, it’ll still work. Now add the following:

print (Monty_Python_Library)

Run those the dictionary code and the print statement and you’ll see the entire dictionary appear in your IDLE Shell. So, that’s easy enough to create (I hope). It’s also easy to add or subtract things from that dictionary. Here’s how you add something:

Monty_Python_Library ['The Gospel According to Charley Drake'] = 'Charley Drake'

Now, if you print the library again, you’ll see the classic by Charley Drake added in.

Here’s how the code looks in my IDLE, though note I’ve added some extra print lines to make it a bit clearer:

But what if you want to get rid of some items? There are a couple of common ways of doing that as well. One uses the term del (as in delete), and the other uses the term pop (as in stick a needle in a balloon). We’ll use both below:

del Monty_Python_Library ['Thirty Days in the Samarkind Desert 
with the Duchess of Kent']
Monty_Python_Library.pop ('Rarnaby Budge')

If you print the dictionary again, those should be missing from the list.

You can now use this dictionary to find out who wrote specific titles. For example, we can use the following line to ask who wrote ‘David Coperfield’. We get, of course, the not-so-renown (except among Monty Python fans) Edmund Wells.

>>>Monty_Python_Library ['David Coperfield']

'Edmund Wells'

If you wanted to expand this library, you could add in more corresponding values to the titles, stuff such as publication date, publishing house, etc.

Monty_Python_Library ['David Coperfield'] = 'Edmund Wells', 'Random House', 1876

Here’s how this coding looks in my IDLE:

There’s plenty more to know about compiling and structuring dictionaries, but that’s enough to cover the basics. If you’ve suddenly developed a passion for dictionaries (and who hasn’t, at some point?), then I recommend the following sources:

Learn Python the Hard Way

Hands-On Python Tutorial

Feature Image: Duchess_of_Kent,_2013 wikimedia The Duke and Duchess of Kent; Image by Carfax2 in Wikimedia https://commons.wikimedia.org/wiki/File:The_Duke_and_Duchess_of_Kent,_2013.JPG

Published by

Mark R. Vickers

I am a writer, analyst, futurist and researcher. I've spent most of my working life as an editor and manager for research organizations focusing on social, business, technology, HR and management trends. But, perhaps more to the point for this blog, I'm curious about the universe and the myriad, often mysterious relationships therein.

One thought on “Is Khristmas in Your Dictionary?”

Leave a Reply