
Python dictionary items() - Get key-value pairs of a dictionary
The items()
returns pairs of a dictionary, each of which is a tuple of a key and value.
d = {'CA': 'California', 'NV': 'Nevada', 'TX': 'Texas'}
items = d.items()
print(items)
# dict_items([('CA', 'California'), ('NV', 'Nevada'), ('TX', 'Texas')])
Each element of a dict_items is a tuple of the dictionary's key and value. It is often used in the for statement.
d = {'CA': 'California', 'NV': 'Nevada', 'TX': 'Texas'}
for item in d.items():
print(item)
# ('CA', 'California')
# ('NV', 'Nevada')
# ('TX', 'Texas')
Comments
Powered by Markdown