SyntaxStudy
Sign Up
Home Python Reference dict.keys() / values() / items()

dict.keys() / values() / items()

method

Return view objects of a dictionary's keys, values, or key-value pairs respectively.

Syntax

dict.keys() dict.values() dict.items()

Example

python
user = {'name':'Alice','age':28,'city':'London'}

print(list(user.keys()))    # ['name','age','city']
print(list(user.values()))  # ['Alice',28,'London']
print(list(user.items()))   # [('name','Alice'),...]

for key, value in user.items():
    print(f'{key}: {value}')