# Python Dictionaries

> Learn how Python dictionaries store key/value pairs: how to read and change values, use get(), pop(), and keys(), and add or remove items from a dictionary.

Author: Flavio Copes | Published: 2020-12-21 | Canonical: https://flaviocopes.com/python-dictionaries/

Dictionaries are a very important [Python](https://flaviocopes.com/python-introduction/) data structure.

While lists allow you to create collections of values, dictionaries allow you to create collections of **key / value pairs**.

Here is a dictionary example with one key/value pair:

```python
dog = { 'name': 'Roger' }
```

The key can be any immutable value like a string, a number or a tuple. The value can be anything you want.

A dictionary can contain multiple key/value pairs:

```python
dog = { 'name': 'Roger', 'age': 8 }
```

You can access individual key values using this notation:

```python
dog['name'] # 'Roger'
dog['age']  # 8
```

Using the same notation you can change the value stored at a specific index:

```python
dog['name'] = 'Syd'
```

And another way is using the `get()` method, which has an option to add a default value:

```python
dog.get('name') # 'Roger'
dog.get('test', 'default') # 'default'
```

The `pop()` method retrieves the value of a key, and subsequently deletes the item from the dictionary:

```python
dog.pop('name') # 'Roger'
```

The `popitem()` method retrieves and removes the last key/value pair inserted into the dictionary:

```python
dog.popitem()
```

You can check if a key is contained into a dictionary with the `in` operator:

```python
'name' in dog # True
```

Get a list with the keys in a dictionary using the `keys()` method, passing its result to the `list()` constructor:

```python
list(dog.keys()) # ['name', 'age']
```

Get the values using the `values()` method, and the key/value pairs tuples using the `items()` method:

```python
print(list(dog.values()))
# ['Roger', 8]

print(list(dog.items()))
# [('name', 'Roger'), ('age', 8)]
```

Get a dictionary length using the `len()` global function, the same we used to get the length of a string or the items in a list:

```python
len(dog) #2
```

You can add a new key/value pair to the dictionary in this way:

```python
dog['favorite food'] = 'Meat'
```

You can remove a key/value pair from a dictionary using the `del` statement:

```python
del dog['favorite food']
```

To copy a dictionary, use the copy() method:

```python
dogCopy = dog.copy()
```
