DEV Community

Main
Main

Posted on • Originally published at pynerds.com on

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

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()
Enter fullscreen mode Exit fullscreen mode

The method does not take any argument.


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

print(all_items)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

iterate through the values


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

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


Top comments (0)