The items method
The items method returns the keys and values of a dictionary as a set of tuples. The result is returned as a special object dict_items. We do not specify anything in the method parameter.
Syntax
dictionary.items()
Example
Let's find all the key-value pairs of our dictionary:
dct = {
'a': 1,
'b': 2,
'c': 3
}
print(dct.items())
Result of code execution:
dict_items([('a', 1), ('b', 2), ('c', 3)])
Example
We get the result as a list of tuples:
dct = {
'a': 1,
'b': 2,
'c': 3
}
print(list(dct.items()))
Result of code execution:
[('a', 1), ('b', 2), ('c', 3)]
See also
-
method
get,
which returns a dictionary value by key -
method
keys,
which returns the keys of the dictionary -
method
setdefault,
which adds a value for the default key -
method
values,
which returns all the values of the dictionary