# Python Dictionaries

A Python dictionary stores items in the form of a key-value pair. The values stored in a dictionary can be changed, but the dictionary itself cannot be duplicated.

Python dictionaries are pretty easy to understand, and any confusion about them will be cleared up in this article.

## Creating a Dictionary

Dictionaries store items as keys that have associated values, which come together to form key-value pairs. The key-value pairs are enclosed in curly braces ({}) and separated from other similar items by commas (,). The keys of the dictionary must be unique and immutable, but this does not apply to the values. For keys to be immutable, they have to be in the form of strings, numbers, or tuples. Look at this dictionary below, which stores the names of popular footballers as keys and their countries of origin as values:

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}
```

## Accessing a Value in the Dictionary

To get a particular value in a dictionary, you will have to access the key that is associated with the value. You can do this by using the get() method. If you provide a key that doesn’t exist, it gives you a value of *none.*

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}
print(origins.get("messi"))
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681140715054/7d9707e0-58d3-4d51-b501-76ab7e0a53be.jpeg align="center")

However, the most popular method of getting values from a dictionary is to use square brackets (\[\]). You simply have to put the key inside the square brackets (\[\]), and it returns the value.

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}

print(origins["neymar"])
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681145687565/9d233773-8d88-4616-88f5-30d95919733c.jpeg align="center")

## Updating Values in a Dictionary

Dictionaries are mutable just like lists and sets, which means that the stored values can be changed. You can change values associated with a key using the update() method. Once you update the values and run the code, it will give you a dictionary with the updated value.

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}
origins.update({"mbappe": "algeria"})
print(origins)
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681141100372/993dc150-631d-4cd1-9de6-f30ef0b0c8d0.jpeg align="center")

## Removing/deleting Items from a Dictionary

There are various methods of removing /deleting items from a dictionary, which you will need to know. These methods include:

1. **pop() method**
    
    This will remove the specified key and its value from the dictionary.
    
    ```python
    origins= {
        "ronaldo": "portugal",
        "messi": "argentina",
        "neymar": "brazil",
        "mbappe": "france"
    }
    origins.pop({"mbappe": "algeria"})
    print(origins)
    ```
    
    **Output:**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681141654144/49e36ada-eafa-4e0b-8a3e-7fd6017080dd.jpeg align="center")
    
2. **popitem() method**
    
    This removes the item that was last added to the dictionary. It doesn’t require any key.
    
    ```python
    origins= {
        "ronaldo": "portugal",
        "messi": "argentina",
        "neymar": "brazil",
        "mbappe": "france"
    }
    origins.popitem()
    print(origins)
    ```
    
    **Output:**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681141931969/f0bb2cc0-a14f-4442-b510-be51a1628ce4.jpeg align="center")
    
3. **clear() method**
    
    It removes every item in a dictionary and simply returns an empty dictionary.
    

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}
origins.clear()
print(origins)
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681142225973/2da446f7-15fb-4cc6-b8c2-4ebac3fb631f.jpeg align="center")

## Getting All the Keys in a Dictionary

You can use the keys() method to access all the keys in a dictionary at once. Once you run the code, it will return an object that looks like a list containing all the keys.

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}

print(origins.keys())
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681142464694/2b2ae072-f9bd-4ad4-bcf9-a6eb3d549feb.jpeg align="center")

You can use a for loop to access the keys individually, just like in the code below:

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}
for key in origins.keys():
    print(key + " is good")
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681142649215/ba37dc4b-8ac1-48f1-94a9-05bf271238c2.jpeg align="center")

## Using the values() method in a Dictionary

The values() method returns all the values in a dictionary at once. It returns an object that resembles a list containing all the values.

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}

print(origins.values())
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681142867212/4218d7e5-3203-43c8-82fa-d685dad8f42e.jpeg align="center")

You can also use a for loop to get the values. Check out this code snippet below:

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}
for key in origins.values():
    print(value + " is a country")
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681143182392/068f3a15-25a0-4fbc-bb7e-e464b25cd337.jpeg align="center")

## Getting Both the Keys and Values Simultaneously

It is possible to get both keys and values at the same time in the dictionary. You will have to use the items() method to carry out this operation. It will return an object that is like a 2D list of tuples and contains both the keys and values.

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}

print(origins.items())
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681143379851/895a97e0-bf45-4bf4-81b5-960bbfa1d3af.jpeg align="center")

The items() method can be manipulated with the use of a for loop to get a key-value pair. You have to use the f string, which provides a way of putting Python expressions inside string literals, and these expressions get replaced by their values.

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}
for key, value in origins.items():
    print(f"{key} : {value}")
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681148220472/6135e32c-3399-4bd8-a67e-14deb21fcb3d.jpeg align="center")

## Creating Nested Dictionaries

A nested dictionary is simply a dictionary within a dictionary, nothing more has to be said.

To access a particular value in a nested dictionary, you will not be able to use the get() method. You will have to use square brackets(\[\]), which will contain the key associated with the item, that has another dictionary as its value. Besides the first set of square brackets, you will need another set of square brackets, which will contain the key in the nested dictionary. This code snippet below will make it all clearer:

```python
players= {
    "player1": {
        "name": "ronaldo",
        "country": "portugal"
    },
    "player2": {
        "name": "messi",
        "country": "argentina"
    },
     "player3": {
         "name": "neymar",
         "country": "brazil"
    },
      "player4": {
          "name": "mbappe",
          "country": "france"
    }
}
print(players["player4"]["country"])
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681144649014/30770ec2-2fff-4944-bce0-60a847b91eb3.jpeg align="center")

As you can see from the code above, there is a dictionary named *players*. Inside the *players* dictionary are nested dictionaries, that contain the names and countries of popular football players. Using the square brackets(\[\]), you can get the required values.

## Accessing Other Dictionary Methods

You can access all the Python dictionary's methods using the dir() method.

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}
print(dir(origins))
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681145366959/d76b2a20-6233-417e-92e9-c27bc3168fb9.jpeg align="center")

You can also check the functions of all these methods using the help() method.

```python
origins= {
    "ronaldo": "portugal",
    "messi": "argentina",
    "neymar": "brazil",
    "mbappe": "france"
}
print(help(origins))
```

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681145463637/7650ab4f-086a-49e5-a963-69bee1208b12.jpeg align="center")

## Conclusion

Once understood perfectly, Python dictionaries can be easy to use and can be applied to a coding project.
