Get Dictionary Keys, Values, or Items in Python

Accessing the keys, values, or items of a dictionary allows you to retrieve specific information from the dictionary. In this blog post, we will explore different methods to get the keys, values, or items of a dictionary in Python.

Method 1: Using the keys(), values(), or items() Methods

Python

Output:

Keys: dict_keys([‘name’, ‘age’, ‘city’])

Values: dict_values([‘John’, 30, ‘New York’])

Items: dict_items([(‘name’, ‘John’), (‘age’, 30), (‘city’, ‘New York’)])

Method 2: Converting to Lists

Python

Output:

Keys as list: [‘name’, ‘age’, ‘city’]

Values as list: [‘John’, 30, ‘New York’]

Items as list: [(‘name’, ‘John’), (‘age’, 30), (‘city’, ‘New York’)]