An element in a dictionary is also called an item. Each item is made up of a key and a value.

Dictionary constituents

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:

Syntax:
d.items()
copy

The method does not take any argument.

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

print(all_items)
copy
Output:
dict_items([('Spain', 'Madrid'), ('Italy', 'Rome'), ('France', 'paris')]) [Finished in 0.010866111144423485s]

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.

ExampleEdit & Run

iterating through a dictionary

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

for i in d:
   print(i)
copy
Output:
Spain Italy France [Finished in 0.010812003165483475s]

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.

ExampleEdit & Run

iterating over items()

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

for i in d.items():
   print(i)
copy
Output:
('Spain', 'Madrid') ('Italy', 'Rome') ('France', 'paris') [Finished in 0.01013611163944006s]

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:

Syntax:
d.keys()
copy

The method does not take any arguments.

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

print(all_keys)
print(type(all_keys))
copy
Output:
dict_keys(['Spain', 'Italy', 'France']) <class 'dict_keys'> [Finished in 0.010421414859592915s]

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

ExampleEdit & Run
d = {
      'Spain': 'Madrid',
      'Italy': 'Rome',
      'France': 'paris'
    }
#iterate through the keys
for k in d.keys():
   print(k)
copy
Output:
Spain Italy France [Finished in 0.01021017786115408s]

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:

Syntax:
d.values()
copy

The method does not take arguments.

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

print(all_values)
print(type(all_values))
copy
Output:
dict_values(['Madrid', 'Rome', 'paris']) <class 'dict_values'> [Finished in 0.011001132428646088s]
ExampleEdit & Run

iterate through the values

d = {
      'Spain': 'Madrid',
      'Italy': 'Rome',
      'France': 'paris'
    }
#iterate through the keys
for v in d.values():
   print(v)
copy
Output:
Madrid Rome paris [Finished in 0.010132506489753723s]

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.