Simple Joys With Python

It’s not always about the big stuff

Butterfly on window pane
Photo by Jian Xhin on Unsplash

Some joys are big and all-consuming, like completing a century bike ride, earning a hard-worked-for promotion, or publishing a blog. And some joys are small and humble, yet make life just as sweet. This week, while playing with the King County data set (yes, another week with this data!) I had a few of my own simple, joyful moments.

I wanted a list of all the unique zip codes from the data set. Not a big ask. Last year, as a Python newbie, I had no idea how to approach the solution and was wasted time overthinking the solution. I recall contemplating a solution of just creating the list manually by typing in every zip code into a list — not cool or efficient.

Result from df.zipcode.unique()

Last week I was determined to use a growth mindset and figure it out. The joy wasn’t that I pulled it off, the joy was that I worked it out quickly and intuitively. Here’s my thought process: I want a list of zip codes; create a list using list(). I want to be able to call the list; set it equal to a variable. I want the list to be the unique set of zip codes; use .unique() and not .nunique() which only returns the number of unique zip codes. I want this data from the data frame; it’s all in the zip code column of the data frame. As I was writing out my line of code, I was stunned by how easily it came to me:

It’s not an earthshattering line of code, yet it was my simple joy. It came naturally and was obvious to me, and that’s what made it so great.

My second simple joy came when I was able to create a new, smaller data frame with each zip code as the index and the mean latitude value, and then convert the data frame into a dictionary.

A lovely dictionary was created.

dictionary from a data frame

The usefulness of the dictionary wasn’t the win; the joy came being able to work easily with the different python collection types.

The final, simple joy came by using a new method that I didn’t have any experience using before; in fact, I’d never heard of the .get() method for python dictionaries yet came across it during a search. It seemed too easy to be true or to be correct syntax. Using the dictionary from above (zip_lat_dict), I wanted to find the value of a certain key — zip code 98039.

The first attempt resulted in “none” which happens if there no value for that particular key. It turns out I called the zip code as a string (in quotation marks) when it was actually an integer. Once the quotation marks were deleted, the correct value for zip code 98039 appeared.

Code snippet in python
Use of .get() method for the dictionary

It was a small, magical moment. And sometimes in life, it’s the small joys — the simple moments of “ah-ha” — that are meaningful.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store