items(), keys() and values() methods in Python dictionary
Published: 2024-04-15 01:34:21

---
title: items(), keys() and values() methods in Python dictionary
published: true
date: 
tags: 
canonical_url: https://www.pynerds.com/items-keys-and-values-methods-in-python-dictionary/
---
An element in a dictionary is also called an  **item**. Each item is made up of a **key** and a **value**.

![Dictionary constituents](https://res.cloudinary.com/dgc50iktu/image/upload/v1706726328/0b233f60-a941-4fd7-8885-60c39de9d3b3_syxepq.png)

The three dictionary methods; `items()`, `keys()` and `values() `retrieves all items, keys and values respectively.

- `items()` - Returns an iterable of all key-value pairs(items) present in the dictionary.
- `keys()` - Returns an iterable with all the keys in the dictionary.
- `values() `- Returns an iterable with all the values in the dictionary.

## items() - Get all items present in a dictionary

To get an iterable containing all the items present in a dictionary, use the` items() `method. The syntax is as shown below:

```

d.items()
```

The method does not take any argument.

```

d = {
      'Spain': 'Madrid',
      'Italy': 'Rome',
      'France': 'paris'
    }
#get the items
all_items = d.items()

print(all_items)
```

The `items() `method is useful when it comes to iterating over the dictionary items. Remember that iterating over a dictionary only yields its keys, so using the `items() `method allows you to access both the key and its corresponding value in one iteration.

iterating through a dictionary

```

d = {
      'Spain': 'Madrid',
      'Italy': 'Rome',
      'France': 'paris'
    }

for i in d:
   print(i)
```

In the above example, we used the for loop to iterate over a dictionary. As you can see, only the keys are returned. Now consider the same example with the `items()` method.

iterating over items()

```

d = {
      'Spain': 'Madrid',
      'Italy': 'Rome',
      'France': 'paris'
    }

for i in d.items():
   print(i)
```

As you can see, iterating through the `items()` object returns tuples in the form `(key, value)` for each dictionary item.

## keys() - get all keys of a dictionary

The` keys() `method returns an object containing all the keys present in a dictionary.

it has the following syntax:

```

d.keys()
```

The method does not take any arguments.

```

d = {
      'Spain': 'Madrid',
      'Italy': 'Rome',
      'France': 'paris'
    }
#get the keys
all_keys = d.keys()

print(all_keys)
print(type(all_keys))
```

You can iterate through the returned object to get individual keys at each iteration.

```

d = {
      'Spain': 'Madrid',
      'Italy': 'Rome',
      'France': 'paris'
    }
#iterate through the keys
for k in d.keys():
   print(k)
```

## values() - get all values in the dictionary

Finally, the `values()` method returns an object containing all the keys in the dictionary. It has the following syntax:

```

d.values()
```

The method does not take arguments.

```

d = {
      'Spain': 'Madrid',
      'Italy': 'Rome',
      'France': 'paris'
    }
#get the keys
all_values = d.values()

print(all_values)
print(type(all_values))
```

iterate through the values

```

d = {
      'Spain': 'Madrid',
      'Italy': 'Rome',
      'France': 'paris'
    }
#iterate through the keys
for v in d.values():
   print(v)
```

## Conclusion

- Use `items() `to get an iterable of two length tuples, where each tuple contains a key and a value of a dictionary item.
- Use `keys()` to get an iterable of all the keys present in a dictionary.
- use `values()` to get an iterable of all the values present in a dictionary.

<div class="related"><h3>Related articles</h3><hr><ul><li><p><a class="text-success" href="https://www.pynerds.com/dictionary-in-python/">Dictionary in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/python-dict-function/">Python dict() Function</a><li><p><a class="text-success" href="https://www.pynerds.com/dict-get-method-in-python/">dict.get() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/dict-update-method-in-python/">dict.update() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/dict-pop-in-python/">dict.pop() in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/dict-clear-method-in-python/">dict.clear() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/dict-fromkeys-method-in-python/">dict.fromkeys() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/dict-popitem-method-in-python/">dict.popitem() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/dict-setdefault-method-in-python/">dict.setdefault() method in Python</a><li><p><a class="text-success" href="https://www.pynerds.com/dict-copy-method-in-python/">dict.copy() method in Python</a></ul></div>