DEV Community

Cover image for Python Dictionary and Methods - Explained
Mikhail Raevskiy
Mikhail Raevskiy

Posted on • Originally published at Medium

Python Dictionary and Methods - Explained

What is a dictionary

A dictionary is an unordered data structure that allows you to store key-value pairs. Here's an example of a Python Dictionary:

Python Dictionaries and Methods

This dictionary uses strings as keys, but the key can be, in principle, any immutable data type. The value of a particular key can be anything. Here's another example of a dictionary where keys are numbers and values are strings:

Python Dictionaries and Methods

An important clarification: if you try to use a mutable data type as a key, you will get an error:

Python Dictionaries and Methods

In fact, the problem is not with mutable data types, but with non-hashable data types, but usually, they are the same thing.

Retrieving data from a dictionary 📖

Square brackets are used to get the value of a particular key []. Suppose we have a pair in our dictionary 'marathon': 26.

Python dictionaries and methods
Again, you will get an error if you try to retrieve a value for a non-existent key. There are methods to avoid such mistakes, which we will talk about now.

Adding and updating keys 🔑

Adding new pairs to the dictionary is quite simple:

Python dictionaries and methods

Updating existing values is exactly the same:

Python dictionaries and methods

Deleting keys

To remove a key and corresponding value from a dictionary one can use del

Python dictionaries and methods


🍴 Methods 🍴

Python dictionaries have many different useful methods to help you in your work with them. Here are just a few of them.

👉 Update

The method update() is useful if you need to update several pairs at once. The method takes another dictionary as an argument.

Python dictionaries and methods

If you are wondering why the data in the dictionary is not in the order in which it was entered, it is because the dictionaries are not ordered.

👉 Get

Let's suppose we have a dictionary:

Python dictionaries and methods

The method get() returns the value for the specified key. If the specified key does not exist, the method will return None.

Python dictionaries and methods

The method can be used to check for the presence of keys in the dictionary:

Python dictionaries and methods

You can also specify a default value that will be returned instead of None if the key is not in the dictionary:

Python dictionaries and methods

👉 Pop

The method pop() deletes the key and returns the corresponding value.

Python dictionaries and methods

👉 Keys

The method keys() returns a collection of keys in the dictionary.

Python dictionaries and methods

👉 Values

The method values() returns the collection of values in the dictionary.

Python dictionaries and methods

👉 Items

The method items() returns key-value pairs.

Python dictionaries and methods

👉 Iterating through a dictionary

You can iterate over each key in the dictionary.

Python dictionaries and methods

Obviously, instead of story_count you can usestory_count.keys().

In the example code below, the loop for uses the methoditems() to get a key-value pair for each iteration.

Python dictionaries and methods

Read More

If you found this article helpful, click the💚 or 👏 button below or share the article on Facebook so your friends can benefit from it too.

https://slidetosubscribe.com/raevskymichail/

Top comments (1)

Collapse
 
gravesli profile image
gravesli

I think you are great! i just want to discuss tech with Python developer.
I built a display machine state using Python3 with Flask!
Flask State Github:github.com/yoobool/flask-state
Should i can get some improvement suggestions from you? Thanks~