📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Уроки Python с нуля / #10 – Словари (dict) и работа с ними

Школа itProger / Программирование16:10

Transcription

Hello! In this lesson, we will learn how to work with dictionaries in Python, as well as explore functions for working with them.

Before we begin, I would like to recommend the website etipragger.com. On this site, you will find homework assignments and a lot of other useful information. The link to this lesson on the site will be in the description of this video.

Earlier, when creating a dictionary or a tuple, we accessed elements by their indices to get their values. In Python, there is a type called dictionaries, which allows us to specify a so-called key, or in other words, a name for a specific value. We can then use this key, or name, to access a specific element and retrieve, modify, delete, and so on.

To create a dictionary, we first need to give it a name. Let's call this dictionary "country." Since I am creating a dictionary, I need to use curly braces. For lists, we use square brackets, and for tuples, we use parentheses, but for dictionaries, we use curly braces.

We cannot just list all the elements right away because we need to add a key to each element. The keys can be of any type, and they can be different data types. For example, I can specify the number 4 as a key for the value 3. In this case, to access this element, I would write "country" and in square brackets, I would specify the number 4. This way, I would access the key and retrieve the value 3.

We can also use booleans as keys. If I run this, I will get the desired value. Strings can also be used as keys, and interestingly, tuples can be used as keys, but lists cannot. So, I can specify a tuple, for example, (5, 6), and then I can also reference this tuple and still access the desired element.

When we create a dictionary, we always specify a key and a value. At the moment, I have created a dictionary that doesn't have much meaning because these are just numbers that don't tell us anything. However, dictionaries are very convenient for describing an object.

For example, if I want to describe a country, I can create a dictionary. Here, I will specify the first key as "code," and for the value, I can use a country code, like "RU" for Russia.

Next, let's create an additional element. To create new elements, we simply separate them with commas. The types of keys can be anything; I could write the number 5 as a key, but that wouldn't tell me much, so I will use a string.

I will name this key "name" and specify the name of the country, for example, "Russia." Additionally, I can specify other characteristics, like "population." I hope that's how it's spelled. Here, I can indicate the population, which I will assume is in hundreds of millions. So, I will specify, for example, 144, meaning 144 million people live in Russia.

You can also specify other characteristics as desired. All these characteristics will have a specific key, or label, and a specific value. When you need to retrieve, say, just the name of the country from this dictionary, you won't have to remember the index of the element you need; you can simply refer to the key.

It's much more convenient and pleasant to specify the key than to remember an index like 543 or something like that. So, we just access the key with the value "name." If I run this, I will get the value for this specific element, and everything is processed correctly.

Additionally, there is an alternative way to write dictionaries. Let's comment out this line for now and recreate everything using a function called "dict." The word "dictionary" translates to "dict" in English, and we can use this function to create a dictionary.

Here, we can specify keys and values, but they are formatted differently. We write the key names as strings. For example, "code" with the value "RU," and then "name" with the value "Russia."

If I run this, everything will be processed correctly. I personally prefer using regular curly braces because with the dict function, you can only use strings as keys, while with curly braces, you can use numbers, tuples, and so on.

Now, let's try to output the entire dictionary. To do this, we can simply refer to "country." If I run this, you will see that everything is processed correctly, and it displays in curly braces, indicating that we are indeed working with a dictionary.

You can also iterate through it using a loop. Let's create a loop. While we could use a while loop, it's much simpler to use a for loop for iterating through dictionaries, lists, and tuples.

So, we will use a for loop to create a variable and specify that we are iterating through the "country" dictionary. I will print this variable to the screen and see what is output.

We can notice that instead of values, we are getting keys. It would make more sense to rename this variable to "key" since we are indeed getting keys. If I want to get not only the keys but also the values, I need to create another variable.

Let's call it "value." I also need to refer to "country" and use the "items" function. Before we output this loop, let's first see what we get from the "items" function.

We can see that we get a list where each element is a tuple consisting of two elements: the key and the value. We can iterate through this list of tuples. I will print the key, add a dash, and then print the value.

If I run this, we see that both the key and the value are displayed correctly. Thus, to iterate through a dictionary, we can use a for loop. If we want to iterate through just the keys, we can refer to the dictionary itself. If we want to iterate through the values, we can use the "values" function.

If we run this, we will get a list where each element is a specific key. If we want to get only the values, we can use the "values" function. If we run this, we will get a list consisting solely of values.

If we want to get both keys and values, we use the "items" function, which we have already worked with. This gives us a list where each element is a tuple consisting of a key and a value.

To update any values, you can use the "update" method, or you can simply use square brackets to specify a key. For example, if I want to update the "code" key with a new value, I can do that.

If I save and run this, you will see that the value for "code" has been changed to "RU." You can update values simply by specifying the key or using the "update" function.

Dictionaries are very convenient for describing a specific object. Let's create a dictionary called "person." I will open curly braces and specify a key, for example, "user1."

Then, I will provide a value, and since I will have many values, I will break this into several lines for better readability. The value will not just be a number; I will specify another dictionary as the value.

This is perfectly valid. You can nest dictionaries within dictionaries, and you can also use lists or tuples as values. For example, I will specify the first name as "John," the last name as "Marley," and the age as 45.

I can also save the address as a list instead of a string. For example, I will write "Moscow" as one of the addresses. Additionally, I can specify the street and apartment number.

I can also specify grades as another dictionary. For example, I will specify "math" with a grade of 5 and "physics" with a grade of 3.

You can create any level of nesting, and you can access any elements within the dictionary. Now, let's try to retrieve a specific value.

To do this, I will refer to the "person" dictionary and specify the first element, "user1." Then, I will need to create another set of square brackets to access the inner dictionary.

I will specify the key "address" and then access the first element of the address list. If I run this, everything will be processed correctly.

If I remove the index 1, I will get the entire tuple. If I remove the address, I will get the entire list. This way, you can create any level of nesting and access any elements within the dictionary, and everything will be processed correctly.

Well, that concludes our lesson. I hope you enjoyed it. If so, don't forget to subscribe to the channel and join our social media. All the links will be in the description of this video. That's all from me. See you soon! Goodbye!