Last Updated:

Dictionary Methods in Python with examples

In our today's article, we will tell you about a fairly popular and widely used type of data called dictionaries. We will also explain in detail and show why these very dictionaries in Python exist and are used. And besides, we will share with you a couple of life hacks and useful tips that will help you quickly deal with all this. It is worth noting that the article is equipped with a bunch of examples, which we tried to carefully and clearly describe.

 

Dictionary Methods in Python

First step: Create a dictionary

 

When working with a dictionary, the first step is to create one. It's not too complicated, you just have to convey the right sequence of the right elements. This sequence is written in these curly braces: { }. Let's take out separately that each element should be separated by commas. As a result, it turns out that each element must have a "key: value" pair.

Please note that the values can be absolutely anything, and in addition, they can even be repeated. But! Most importantly, your key must be made unique.

The very first case is how to create a completely empty dictionary in Python:

dict_sample = { }

And here is an example of a dictionary where the keys will be specified in integers:

dict_sample = {1: ‘cat’, 2: ‘dog’}

Another example where there are keys that have different types:

dict_sample = {‘animal’: ‘cat’, 1: [2, 3, 5]}

Alternatively, you can make a dictionary using dict ( ):

dict_sample = dict({2:’cat’, 4:’dog’})

There's still a way to use consistency. Catch our example:

dict_sample = dict([(2,’cat’), (4,’dog’)])

In order to output the contents of the dictionary, you need to use the print ( ) function. After that, the name of the dictionary must be set as an argument. To make it clearer to you, here is an example:

dict_sample = {«Name»: «Ivan»,

«Surname»: «Ivanov»,

«Age»: 99}

print (dict_sample)

As a result, we get the following result:

{‘Name’: ‘Ivan’, ‘Surname’: ‘Ivanov’, ‘Age’: 99}

How to access items

If you want to access the elements of any dictionary, you should write the key in square brackets [ ]. Here's an example:

dict_sample = { «Name»: «Ivan»,

«Surname»: «Petrov»}

x = dict_sample[«Surname»]

print (x)

In the conclusion we get:

Petrov

Programmers can also use the get ( ) function if they want to access selected dictionaries. Get ( ) is added through a period, and then simply set the name of the desired dictionary as a function. See how:

dict_sample = {«Name»: «Ivan»,

«Surname»: «Ivanov«,

«Age»: 99}

x = dict_sample.get(«Surname»)

print (x)

The result is this:

Ivanov

Now you already understand how to quickly access the various elements in your dictionary. Then let's move on.

Step Two: Add Items

 

Unsurprisingly, there are many different ways to add new elements to the dictionary. To get started, you can select a new key and then assign it a new value. See how one of the ways looks:

dict_sample = {«Name»: «Ivan»,

«Surname»: «Ivanov»,

«Age»: 99}

print (dict_sample)

dict_sample[«Country»] = «Russia»

print (dict_sample)

As a result, we derive the following result:

{‘Country’: ‘Russia’, ‘Name’: ‘Ivan’, ‘Surname’: ‘Ivanov’, ‘Age’: 99}

Note that a new item in the dictionary is always displayed first.

There is another way. First of all, you create an empty dictionary (instructions on how to do this are above):

MyDictionary = { }

print(«List: «)

print(MyDictionary)

We get the result:

List:

Note that the dictionary does not output anything. This is because it does not yet have any values.

Now gradually we can introduce new elements:

MyDictionary[1] = ‘Cats’

MyDictionary[2] = ‘Dogs’

print(«\n2 things have been input: «)

print(MyDictionary)

And here's what we get after all the changes:

2 things have been input: {1: ‘Cats’, 2: ‘Dogs’}

Updating Items

 

When you add an item to a dictionary, you immediately get the ability to change it. Don't worry: it's easy. To perform this operation, you just need to use the appropriate key. Let's look at an example to make it clearer:

dict_sample = {«Name»: «Ivan»,

«Surname»: «Ivanov»,

«Age»: 99}

print (dict_sample)

dict_sample[«Country»] = «Ukraine»

print(dict_sample)

And here is our result:

{‘Country’: ‘Ukraine’, ‘Name’: ‘Ivan’, ‘Surname’: ‘Ivanov’, ‘Age’: 99}

Here we see that the meaning of the key "Country" has changed from Russia to Ukraine.

Final Step: Delete Items

As well as adding, erasing elements of the dictionary, you can not in one way.

Let's get acquainted with the most used:

Using the word del. With it, you can erase an item that has a specific key. Let's look at our case:

dict_sample = {«Name»: «Ivan»,

«Surname»: «Ivanov»,

«Age»: 99}

del dict_sample[«Age»]

print(dict_sample)

As a result, we get:

{‘Name’: ‘Ivan’, ‘Surname’: ‘Ivanov’}

That is, you enter the word del, and immediately after it - the name of your dictionary. Then you specify the key of the item you want to remove. We gave the example of "Age", so this entry is erased from the dictionary.

Dictionary methods in Python

Here we have collected only a small part of the useful functions that can greatly facilitate your work with dictionaries in Python.

dict.clear ( ) – so you can completely clear your entire vocabulary

dict.copy ( ) — with its help, the user displays a copy of your dictionary.

dict.get (key[, default]) — Using this combination, you can output the value of your key. Note, however, that if it is not present, default will be displayed.

dict.items ( ) — outputs pairs (both key and value).

dict.values ( ) — this function will return the values in the dictionary.

dict.keys ( ) — displays the keys in the dictionary.

We think we've put everything on the shelves and so you'll be much better at creating and working with dictionaries in Python. But as everyone knows, theory is best reinforced by practice, so rather try and create!