Transcripted Summary

In this chapter, we'll learn about dictionaries.

Dictionaries are a type of Python data collection that stores the data in key/value pairs.



  • The keys are generally made of Strings, integers, or tuples, and need to be both unique and immutable.

  • The values can be set to any data type and any number of key/value pairs can be contained in a dictionary.

  • You can make an empty dictionary by assigning a dictionary name with nothing in the curly braces or your dictionary can contain millions of entries.

  • Dictionaries are mutable and can be changed using the Python dictionary methods.

  • As of Python 3.7, the order of a dictionary can be maintained.

Dictionaries are created with curly braces and each key is separated from each value by a colon. Each key/value pair is separated from each other key/value pair with a comma.


In our sample dictionary called “years”, you'll see that “years” is the name of the dictionary, and that is set equal to curly braces that contain two key/value pairs.

  • The first key is “Layla” and the second key is “Ackeem”. Those keys are unique, they are Strings, and in this instance, they're names.

  • The value for each of these keys is the year. The value for “Layla” is “1974” and the value for “Ackeem” is “1997”.


# Python Dictionary Methods

Let's go ahead and try to make some dictionaries on our own and use some of the Python dictionary methods to work with the information in our dictionaries.

An empty dictionary can be created by making the variable that you'd like to name the dictionary and then using 2 empty curly braces to specify that the dictionary is available and can have information added.

We'll go ahead and put information into our dictionary.

Python has many methods to interact with dictionaries. Let's take a look at how some of these methods work.


First, we'll take a look at get.

By using the stuff.get method, we can return the value of one of the keys in the dictionary.

If we want to know what the value of “food” is, we can enter “food” in the function — stuff.get(“food”).

Let's print the output of stuff.get.


stuff = {"food": 15, "energy": 100, "enemies":3}
print(stuff.get("food"))

Our output is 15. That's correct.

So, we can find the value of any key in the dictionary, which with such a small dictionary seems to be a little bit pointless at the moment. But if you're working with large key/value pairs and large dictionaries, this could be incredibly useful.


Now we are going to use the items method.

The items method takes the name of the dictionary and outputs a view of the key/value pairs.


print(stuff.items())

We can leave the parentheses empty because the items method accepts the dictionary as the argument and acts on the entire dictionary.

You'll notice that the items are listed out in order.



Our next method is the keys method.

The keys method returns a view of all of the keys in the dictionary.


print(stuff.keys())

The keys method, like the items method, accepts the whole dictionary to act upon as an argument.



So, you'll notice that our items give us a key and value pair while the keys give us a keys only.

Now that we've taken a look at how to find out what's in the dictionary, we're going to take a look at how to remove items from a dictionary and insert keys and values if they're not present.


First, we're going to look at popitem.

The popitem method allows us to remove the last item in a dictionary.

Because this is the case, we don't have to give an argument to the popitem method.


print(stuff.popitem())
print(stuff)

You can go ahead and run this code and we'll see that the item has been removed.


Next up is setdefault.

The setdefault method allows us to see what the value is of a key that is in the dictionary, but more importantly, allows us to set a default value when a key is not in the dictionary and to add that value to the dictionary.

Let's take a look at our dictionary and try out the setdefault method.

I'm going to comment out my popitem lines so that way that only the lines related to setdefault print in my output.


print(stuff.setdefault("food"))
print(stuff)
print(stuff.setdefault("friends", 123))
print(stuff)

I'll go ahead and expand my terminal so I can watch the code run.



You'll notice that the setdefault("food") is 15, which is the value of the “food” in the dictionary.

You'll also notice that because I commented out my popitem, my “enemies” are back in the dictionary.

Now you'll notice that the default for “friends” is “123”, and you'll notice that the setdefault method has added “friends” to the dictionary and set the value to a default of “123”.


The last thing that we'll look at is the Python dictionary update method.

First, I'll go ahead and comment out these lines so that way that only our update code will run.

As a reminder, our first dictionary is a dictionary of stuff that we might find if we were making a video game. We're going to update this dictionary of stuff through the update method; so we’ll go ahead and see how that works.

One of the ways that you can use the update method is to update the first dictionary with another dictionary.

  • So, in order to show you how this works, I'll make another dictionary called “new_items”.

  • Now, I will call the update method on the first dictionary. And as an argument, I'll add the second dictionary.

  • Finally, I will print the name of the first dictionary which should be updated with the items from the second dictionary.


new_items = {"rocks": 4, "arrows": 18}
stuff.update(new_items)
print(stuff)

So, let's see if that holds true.

Here we are.



Now our dictionary contains “food”, “energy”, and “enemies”, as well as “rocks” and “arrows”.


We can update existing items in the dictionary through the same process.

So, for example, if I want to update the “rocks” and “arrows”, one way that I can do it is to change those right in the “new_items” dictionary.

Now, I can call the update method in the same way I did before.


new_items = {"rocks": 2, "arrows":5}
stuff.update(new_items)
print(stuff)

When I print the second, then I should see the update of these items in the dictionary.



And you'll notice that we added our items and then we were able to update the items again.


Let's see if we can update and add new items at the same time with the update method.


up_new = {"food": 3, "webs":2}
stuff.update(up_new)
print(stuff)

We can.

Finally, you can add items directly to the update method in order to use it.

So, if we'd like to update our “food” (without quotes in the code), we can enter the name and then we can assign “food” a new value.


stuff.update(food = 450, cookies = 6)
print(stuff)

You'll notice that by adding the argument directly to the update method, we are able to update without having to make a second dictionary.

We can also add new items to the dictionary by adding those items directly as arguments to the update method.



I hope this helps you to understand some of the many ways that we can work with dictionaries and some of the most common.



Resources



© 2021 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR