Python Dictionary and Methods - Explained
Tags: python, webdev, datascience, beginners
Published: 2020-11-11 17:03:44


## ❓**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](https://dev-to-uploads.s3.amazonaws.com/i/kygipywvq0p1z8exg5c8.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/cswufrizr3xejxab0071.png)

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

![Python Dictionaries and Methods](https://dev-to-uploads.s3.amazonaws.com/i/9hg6qfebvai4mfttjyo5.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/p0387v4oj13ltxppep41.png)
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](https://dev-to-uploads.s3.amazonaws.com/i/ufwv01o7xi170cuy7bxl.png)

Updating existing values is exactly the same:

![Python dictionaries and methods](https://dev-to-uploads.s3.amazonaws.com/i/krjq5boo1d91tbe7awyh.png)

### **Deleting keys** ❌

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

![Python dictionaries and methods](https://dev-to-uploads.s3.amazonaws.com/i/6eqtr9v2h3lb07qzrc64.png)

---

## **🍴 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](https://dev-to-uploads.s3.amazonaws.com/i/6grspsihelbq0vm1zmyt.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/6uc95x02n01lj5qq61qs.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/jeoedscgumi5va5bd639.png)

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

![Python dictionaries and methods](https://dev-to-uploads.s3.amazonaws.com/i/odx786fdmvs1nlznzg5n.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/ifj7badjrstuqmyjwqnk.png)

### 👉 **Pop**

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

![Python dictionaries and methods](https://dev-to-uploads.s3.amazonaws.com/i/dajfqnjg5dai2j7gj5cb.png)

### 👉 **Keys**

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

![Python dictionaries and methods](https://dev-to-uploads.s3.amazonaws.com/i/9kzzczh0bcdcytbqj9vm.png)

### 👉 **Values**

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

![Python dictionaries and methods](https://dev-to-uploads.s3.amazonaws.com/i/7ap54edwdftz7zu5xkwa.png)

### 👉 **Items**

The method `items()` returns key-value pairs.

![Python dictionaries and methods](https://dev-to-uploads.s3.amazonaws.com/i/g4idx4zx56phrprtrb9u.png)

## 👉 **Iterating through a dictionary**

You can iterate over each key in the dictionary.

![Python dictionaries and methods](https://dev-to-uploads.s3.amazonaws.com/i/g4idx4zx56phrprtrb9u.png)

Obviously, instead of `story_count` you can use`story_count.keys()`.

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

![Python dictionaries and methods](https://dev-to-uploads.s3.amazonaws.com/i/ntj1pozsz7aqsdb6wtrw.png)

# **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/](https://slidetosubscribe.com/raevskymichail/)