How to Use Dictionary keys() Method in PythonPython Basics


The keys() method returns a view object which displays a list of all the keys in the dictionary.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

dict1 = {'Name': 'Volkswagen', 'Vehicle type': 'SUV','Turbo':'no','Price': 150000,'gearbox': 'automatic-shift'}
print(dict1)
print(dict1.keys())
print(list(dict1.keys()))

Output:

{'Name': 'Volkswagen', 'Vehicle type': 'SUV', 'Turbo': 'no', 'Price': 150000, 'gearbox': 'automatic-shift'}
dict_keys(['Name', 'Vehicle type', 'Turbo', 'Price', 'gearbox'])
['Name', 'Vehicle type', 'Turbo', 'Price', 'gearbox']

Syntax

dictionary.keys()

Parameters

This method doesn’t take any parameters.

Return Value

It returns a view object which shows a list of all the dictionary keys.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us