Guide to Creating a Dictionary in Python

Learn how to create and use dictionaries in Python and why they provide an ideal starting point for new programmers.

Pierre DeBois

October 31, 2024

10 Min View
ITPro Today

Python dictionaries are an excellent entry point for new programmers, and in this video tutorial, Pierre DeBois highlights their everyday applications, flexibility, and ability to simplify data organization and processing.

In the video, DeBois demonstrates how to create and use dictionaries in Python, and for those learning Python, why they can help you grasp the key concepts behind the programming language.

The following transcript has been edited for clarity and length.

Subscribe to ITPro Today's YouTube channel for Python tutorials and more.

Transcript:

Pierre DeBois: Hi, I'm Pierre DeBois, founder of Zamana Analytics, and a contributor to ITPro Today. And today, I'd like to talk to you about Python, or, more specifically, dictionaries.

Now, you may have seen dictionaries before, but I think, honestly, it's probably one of the best places to start if you are new to Python. It's basically a container of key-value pairs. And the reason why I think that it's a really great place to start is that those key-value pairs are often things that we see in everyday life. We see this in business, we see this in nonprofits, and pretty much everywhere. And so it's a great way of trying to understand how to construct either software or maybe a dashboard for a data model or, these days, even learning a little bit about large language models.

Related:Python Commands Cheat Sheet

Let's take a closer look. So what I've done here is, within RStudio, I've created this very, very simple script:

Python dictionary simple script

And I'm just going to go ahead and run it, and you'll see here that it's going to use reticulate to run the Python script within RStudio.

Python2.png

And I'm going to enter in the key-value pairs for cities. This is basically cities and temperatures in Fahrenheit. And these are just a couple of cities — New York, Boston, San Francisco, Chicago, and Houston — each with a temperature associated with it. So one of the values of the key-value pair within dictionaries is that it assigns a key to a value. There's no such thing as having two values assigned to the same key within a dictionary. So there's a certain order to this.

If we want to see it, we can always use the print function to call our dictionary object, in this case, cities_in_F. As you can see here, you list out all the entries that we have for key-value pairs.

Houston95.png

Temperatures is not the only application for dictionaries. You can have stock prices. And a quick example here, I'm going to go ahead and run. These are stock prices for Ford, Apple, and Alphabet. And again, you can run a print to take a look at those.

Related:How to Run Python in RStudio Using the Reticulate Library in R

example2.jpeg

And keep in mind the values in your dictionaries do not have to be just something numeric or even characters. It could be a mix. I put this example for cars in here so you can see this. So this is one of my favorite cars, the Chevrolet Corvette. I have make, model, the model year, and I've put in here engine size. The engine size on the newest one, the ZR one, which is a trim level I'm indicating here, has a 5.5-liter v8 engine.

Python dictionary car example

If you put this in here, you can see that all this is a mix of characters and also some numeric in there. So there's some different values you can have in a dictionary.

mix.png

Let's say that we have another list of dictionaries that we want to add. Let's go back to our cities example. You can see here I've put some additional cities — Miami, Dallas, Seattle, Atlanta, Charlotte. OK, so we create our list. Now we can mix and merge these together using a merge function. This is what line 39 is about.

line39.png

Each one of our dictionaries has a double asterisk in front of them. Those double asterisks are telling Python to merge these together, and we're going to assign them into one dictionary, all_cities. Let's run our print. You can see both dictionaries combined and merged into one.

all_cities.jpeg

Now if you want to call a particular value within a dictionary, the syntax for this is just the dictionary, and then the brackets and then the name of the key. So if I call Chicago, for example, for the value x, and print it, this is what the temperature is in Chicago, so it gets that value.

80degree-Chicago.png

And keep in mind that variable x can always be updated with another key and value. So on line 44, if I run for Miami, I'd get Miami's temperature, 95 degrees.

Miami.png

Now, to play around with dictionaries, there are methods that will allow you to actually go through and call either a key or a value, or even to add. And there are several of them. I just want to go through a few of these so you get the idea. But the basic structure is the dictionary, a dot, and then the method itself. So for example, right here in line 49 if I am calling our all_cities_in_F dictionary, and I want to get the value of a key, in this case, Chicago, if I run that, two things will happen. First of all, it will get that actual value, which, again, you can see here was 80, which like we did back a little while ago.

line49.png

But if you notice, I don't have to call out the print function to have it appear. It's just going to do that automatically. And that's how these methods are designed.

And there's several different types of methods that you can learn online. There's ones for keys, where you get all the keys of a dictionary. So if you wanted to go through that. Here are all our keys appearing right here.

keys.png

If we want to get all the values, that's possible. Again, we get all the values within our dictionary.

values.png

How to Update a Dictionary

We can also update a dictionary. There's two ways of doing it. The most basic way it's just calling out the specific key and then setting an equal sign to the new value we want. So let's say for Chicago, since we find out that the temperature is really 70 and not 80, we can just assign 70 to our key, like so, and then when we print it, you'll see that Chicago is called out at 70 among all our among our key values.

Chicago70.png

The other way is to also use the update method. So again, we have "dictionary".update, and then we have our key "Chicago." And this time we find out that we should have had 75 — we keep playing around with a number. So in this case, when we call it, you'll see that Chicago is called out at 75.

Chicago75.png

You can also remove an existing key-value pair. So we can remove Chicago by using our "dictionary". and then our method "pop." We take that out. And you can see that Chicago's taken out of our all cities in Fahrenheit.

remove-city.png

And then we can also add a key-value pair again. Let's see that we wanted to add Santa Fe and Cleveland. We can add a series of these. So if we run a print, you can see that that Santa Fe and Cleveland are added at the very end of our key-value pair.

Santa-Fe.png

Now let's scroll down a little bit. Actually before we do I'm going to add Cincinnati into that.

So one of the things you can do with dictionaries is to run calculations when you need to. For example, our temperatures for each cities are in Fahrenheit, and we might want to have them in Celsius. So with a dictionary comprehension function, we can actually make a conversion, not only taking each key and value pair and converting each from degrees Fahrenheit to degrees Celsius. So running lines 72 through 75 I've just run for each one of our key-value pairs. And as you can see, when I print it, it gives me the temperatures in degrees Celsius.

Celsius.png

So with some dictionaries, you have a number of different ways of running not only calculations, but doing it in memory such that it saves some time and saves some complications when there's a lot of calculations to be done. And we're seeing some applications for this for large language models. For example, if you were to use LangChain, many times the variables are past, prompts are usually dictionaries. There's also memory components that rely on the dictionary structure. So it makes it a little easier to do certain calculations, and also to have some memory when people are making prompts at the line.

And finally, when you're using LangGraph, the nodes that are created also rely on dictionaries, so they use those as a key and then value to put some sort of identification for a node and some sort of value associated with that node.

So if you're learning Python, give dictionaries a try, you'll find it much easier to grasp the key concepts behind Python and be able to build your knowledge base.

About the Author

Pierre DeBois

Pierre DeBois is the founder of Zimana, a small business analytics consultancy that reviews data from Web analytics and social media dashboard solutions, then provides recommendations and Web development action that improves marketing strategy and business profitability. He has conducted analysis for various small businesses and has also provided his business and engineering acumen at various corporations such as Ford Motor Co.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like