keys(): a view object that displays a list of all keys

keys(), takes no argument ( inputs ) .

Returns : the view object as a list of keys of dictionary.

The original dictionary remain same without any change.
my_dict={'a':'One','b':'Two','c':'Three'}
k=my_dict.keys()
print(k)
Output is here
dict_keys(['a', 'b', 'c'])

View object

The view object created by keys() method will reflect the changes in dictionary.
my_dict={'a':'One','b':'Two','c':'Three'}
k=my_dict.keys()
print(k)
my_dict['d']='Four' # one key value pair added
print(k)   # changes are reflected. 
Output
dict_keys(['a', 'b', 'c'])
dict_keys(['a', 'b', 'c', 'd'])

Example for Iterating Through Keys:

Code to loop through the keys in a dictionary.
my_dict = {'a': 'One', 'b': 'Two', 'c': 'Three'}
for key in my_dict.keys():
    print(key)
Use Case: Checking If a Key Exists:
Example of checking if a key exists in a dictionary using keys().
if 'b' in my_dict.keys():
    print("Key 'b' is present")
Difference Between keys() and Direct Access:
keys() returns a view object while direct access (my_dict['key']) looks up a specific key-value pair.
All dictionary methods
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer