# Activity #25 : Research Use Cases of Python Dictionaries Data Structures

Look up how Python dictionaries are used in programming. A dictionary is a data structure that stores information as key-value pairs. Each key is linked to a specific value, and you can use the key to get the value quickly.

### What Is a Dictionary in Python?

A Python dictionary is a data structure that allows us to easily write very efficient code. In many other languages, this data structure is called a **hash table** because its keys are hashable. We'll understand in a bit what this means.

A Python dictionary is a collection of `key:value` pairs.You can think about them as words and their meaning in an ordinary dictionary. Values are said to be *mapped* to keys. For example, in a physical dictionary, the definition *science that searches for patterns in complex data using computer methods* is mapped to the key *Data Science*.

In this Python tutorial, you'll learn how to create a Python dictionary, how to use its methods, and *dictionary comprehension*, as well as which is better: a dictionary or a list. To get the most out of this tutorial, you should be already familiar with Python lists, for loops, conditional statements, and reading datasets with the `reader()` method. If you aren't, you can learn more at [Dataquest](https://www.dataquest.io/data-science-courses/).

### What Are Python Dictionaries Used for?

Python dictionaries allow us to associate a value to a **unique** key, and then to **quickly access this value**. It's a good idea to use them whenever we want to find (lookup for) a certain Python object. We can also use lists for this scope, but they are much slower than dictionaries.

This speed is due to the fact that dictionary keys are **hashable**. Every *immutable* object in Python is hashable, so we can pass it to the `hash()` function, which will return the **hash value** of this object. These values are then used to lookup for a value associated with its unique key. See the example of the use of the `hash()` function below:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728733036386/50998959-bed2-400a-8a8a-9f7c08fe10e4.png align="center")

The string `b` has a hash value of `2132352943288137677`. This value may be different in your case.

### How to Create a Dictionary?

But let's stop with the theory and go straight to the dictionary creation. We have two main methods to define a dictionary: with curly braces `{}` or using the `dict()` method. We'll create two empty dictionaries:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728733115412/51021681-735e-4801-a9b9-6deeba9b11b6.png align="center")

We can see that both dictionaries have the same data type and are equivalent. Now let's populate a dictionary with keys and values. We can do it by using squared brackets, like this `dictionary[key] = value`. We can then access the value by using *bracket notation* with the key we want the value of between the brackets: `dictionary[key]`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728733146042/4b563caf-be79-4eba-90ec-246e74154f8a.png align="center")

The value of `key1` is returned. We can also create a prepopulated dictionary using the syntax below:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728733187024/0b42fc86-e752-45c9-a742-0c3e43d33c56.png align="center")

Ultimately, another method is using `dict()`, in which we supply keys and values as a keyword argument list or as a list of tuples:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728733224480/f6c0bee6-813f-48b1-94e1-38d4316e293c.png align="center")

We used the string data type for the key and the value, but what are the other admissible data types? In Python dictionaries, keys should be *hashable* objects (even if it's not technically correct, we can also say that the objects should be *immutable*). Thus, mutable data types like lists, aren't allowed. Let's try to `hash()` different data types and see what happens:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728733254994/4cf697f7-7067-4fa5-bc23-c40a0121f442.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728733274113/5731d7c5-3222-44b8-bfdb-2785c5a0862f.png align="center")

**Integers, floats, strings, and tuples are hashable data types** (and they are also *immutable*) while **lists are an unhashable data type** (and they are *mutable*). Python uses *hash values* to quickly access a dictionary's values.

On the other hand, values can be of whatever type. Let's add more elements to the dictionary using different data types:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728733333290/b4f40463-de6b-4dfb-b7e9-b4e6eed7e382.png align="center")

---

```python
{'key1': 'value1', 'key2': 'value2', 42: 'the answer to the ultimate question of life, the universe, and everything.', 1.2: ['one point two'], 'list': ['just', 'a', 'list', 'with', 'an', 'integer', 3]{'key1': 'value1', 'key2': 'value2', 42: 'the answer to the ultimate question of life, the universe, and everything.', 1.2: ['one point two'], 'list': ['just', 'a', 'list', 'with', 'an', 'integer', 3]
```

Additionally, we can modify the value of a key with *bracket notation* that we used to populate a dictionary:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728733554886/a6916b46-d26e-4572-b4e2-e536c0665e1f.png align="center")

```python
{'key1': 'value1', 'key2': 'value2', 42: 'the answer to the ultimate question of life, the universe, and everything.', 1.2: ['one point two'], 'list': ["it's another", 'list']}

["it's another", 'list']
```

**References:**

[https://www.dataquest.io/blog/python-dictionaries/#:~:text=A%20Python%20dictionary%20is%20a,collection%20of%20key%3Avalue%20pairs](https://www.dataquest.io/blog/python-dictionaries/#:~:text=A%20Python%20dictionary%20is%20a,collection%20of%20key%3Avalue%20pairs).

[https://www.dataquest.io/blog/python-dictionaries/#:~:text=A%20Python%20dictionary%20is%20a,collection%20of%20key%3Avalue%20pairs](https://www.dataquest.io/blog/python-dictionaries/#:~:text=A%20Python%20dictionary%20is%20a,collection%20of%20key%3Avalue%20pairs).
