Python Dictionary Keys() Method

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview:

In Python Programming, a dictionary holds the mapping of keys and values. Each key is separated from its value by a colon(:). An item has a key and a value is stated as a pair (key : value).

Syntax of Python Dictionary keys()

The syntax of keys() method in Python is as given below:

Where dict is the name of the dictionary.

Parameters of Python Dictionary keys()

The keys() method in Python don't take any parameters.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Return Value of Python Dictionary keys()

keys() method in Python returns a view object that displays all the keys. This view object changes according to the changes in the dictionary.

Example of Python Dictionary keys()

Code:

Output:

What is keys() method in Python?

The Python keys() method is used to retrieve all of the keys from the dictionary. keys() method in Python dictionary returns a list of all the available keys in the dictionary, or an empty list if the dictionary is empty.

The keys in the dictionary must be of an immutable type (string, number, or tuple with immutable elements) and unique, while the values in the dictionary can be of any data type and can repeat.

More Examples

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example 1: To show the working of keys() method in the Dictionary

Code:

Output:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example 2: To demonstrate how the Dictionary is updated

Code:

Output:

Exaplanation:

First, we have taken a dictionary having some keys and values. After that, we have passed one key-value pair into the update function. The update function will update the key-value pair in the original dictionary. Here when the dictionary is updated, keys are automatically updated.

Example 3: Iterating using Key and Value

Code:

Output:

Example 4: Demonstrating the Practical Application of keys()

Code:

Output:

2nd-approach is raising an error because dict_keys in Python 3 do not support indexing.

Conclusion:

  • keys() method in Python is used to retrieve all of the keys from the dictionary.
  • The keys must be of an immutable type (string, number, or tuple with immutable elements) and must be unique.
  • Each key is separated from its value by a colon(:).
  • An item has a key and a value is stated as a pair (key : pair).

See also:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more