Dictionaries in Python

Developed with
QA

Contents

keyboard_tab

The course is part of this learning path

Practical Data Science with Python
course-steps 3 certification 4 lab-steps 3
play-arrow
Dictionaries in Python
Overview
DifficultyAdvanced
Duration49m
Students16

Description

Continuing on from Practical Data Science with Python, this course explores a variety of Python features in a practical, hands-on way. It starts by looking at Python functions and you are given a guided walkthrough of a range of functions relating to data science. It then moves on to dictionaries in Python and how to create them. After that, you'll be guided through flow control in Python, loops, and finally, you'll be shown a technical demonstration in Python looking at classes, variables, and stringification.

Learning Objectives

The main objective of this course is to enhance your knowledge of Python, and learn about Python functions, loops, dictionaries, and flow control.

Intended Audience

This course is intended for IT professionals who already have a good knowledge of Python and who want to enhance that knowledge from a data science perspective.

 

Transcript

Hello and welcome back. I want to speak a little bit about dictionaries. Dictionaries come up quite often in Python, because we'll find ourselves quite frequently working with JSON data and JSON data is pretty much the exact same as a dictionary. You can think of JSON and dictionaries as the same thing.

So we'll learn what JSON looks like and we're also going to learn what dictionaries look like. I'm going to define a nice dictionary here called user. User is going to consist of a key name with a value Thomas. It's going to consist of an age key with a key of 24. I'm going to have a location key as well, which is the UK. So this is a nice and simple dictionary.

So you might now be asking what syntactical element is different between this and a set? If I look at my set, you can see that it's given by name, age, and we have location in there as well. What different syntactical elements can you see? So you can see the colon. So the way that Python is able to distinguish between a set and a dictionary is simply the addition of a colon in its structure. The colon says that the thing on the left-hand side is a key, and the thing on the right hand side is a value.

So with regards to sets as well, something we should note about sets is that they are not ordered. If I try to ask for the zero element of a set, I get the tailing off, because they're just collections of unique elements, and they don't have an order. You can't loop over a set for example. So that's something else to bear in mind.

If I wanted to access an element of my user dictionary, if I wanted to pull out the value 24, for example, I would have to specify age. So instead of having a numeric index, I now defined essentially my own index here, which is just some strings that allow me to access the data associated with that string. And I could have something of an arbitrary complexity.

I could have ages. I could have a list of all ages associated with this key. So, 77, 82, nine. I can put out as many things I want. As long as it's one collection that I can pull out. So the restrictions placed on dictionary keys is that they must be unique, and they must be immutable. Immutable because if you can change the value in memory, then it wreck havoc on a lot of Python programs. You need something that's not going to change in memory from the moment it is born, to the moment it vanishes from the stack. And for that reason, strings are quite often used for that.

So you can almost think of this as being analogous to a table. We can think of this as being analogous to table data, because the key is essentially a table, a column in a table. The values of the data associated with that column.

This is exactly what JSON looks like. JSON tends to be slightly more structured where you would have a key and then you would have another collection of keys and values associated with the key, but they are very much similar. So, this is our dictionary. For that reason, if we try to do things like ask for a minus oneth element to a dictionary, there's no such thing. Dictionaries are also not ordered. They're just grouping of keys and values that have been associated with one another.

If you treat this as a list and you wanted the minus one elements out, what would you do? You would do square brackets minus one, because again what we are doing is we are working with the result of each of these operations. We are now just working with the data, we can then use it as if it's a list. So the restriction placed upon our keys is simply that they must be immutable. So I could have location 99 as a perfectly valid key here. I would have to specify the tuples saying location and 99, and that would give me the value UK. This is acceptable because tuples are immutable objects.

If I did this with a list, I would get an error of sorts, an unhashable tag because we're not allowed to have a list, because lists can change. So, it doesn't make sense for us to have something that can change in memory as a key, because who knows when it's going to change.

Okay, so, these are dictionaries. We will see the dictionaries are quite commonly used to create things called data frames and tandems, but we'll see that when we come across it. Now, I want you to create a dictionary containing foods you like and a list of foods you don't like. You can have a key likes and a list of the names of the foods you like, and then another key dislikes, with a list of foods that you dislike. The key aspects of a dictionary are given by brackets keys and values. Don't call your dictionary dict, because it goes green and that means that you break Python a little bit. So, don't do that. So here we've got an individual dictionary for our likes. So, be it food. So, here is an example of a way to do that, just extending the fact that we can have dictionaries of dictionaries of dictionaries. If you are working with JSON, there is useful library called pprint, which will make JSON data and dictionary a bit nicer. Pprint means pretty print. Now you didn't have to do it like this. I have just demonstrated it like this.

About the Author

Delivering training and developing courseware for multiple aspects across Data Science curriculum, constantly updating and adapting to new trends and methods.

Covered Topics