items(), keys() and values() methods in Python dictionary

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.

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 theitems()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

Thekeys()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.

Related articles

Dictionary in Python

Python dict() Function

dict.get() method in Python

dict.update() method in Python

dict.pop() in Python

dict.clear() method in Python

dict.fromkeys() method in Python

dict.popitem() method in Python

dict.setdefault() method in Python

dict.copy() method in Python

Leave a Reply

Your email address will not be published. Required fields are marked *