In this tutorial, we will look at how to get the keys of a Python dictionary with the help of some examples.
How to get the keys of a dictionary in Python?
You can use the Python dictionary keys()
function to get all the keys in a Python dictionary. The following is the syntax:
# get all the keys in a dictionary sample_dict.keys()
It returns a dict_keys
object containing the keys of the dictionary. This object is iterable, that is, you can use it to iterate through the keys in the dictionary. You can use the list()
function to convert this object into a list.
Let’s look at some examples.
Using dictionary keys()
method
Let’s create a dictionary containing the names to department mappings of employees at an office. The keys here are the employee names whereas the values are their respective departments.
Let’s get all the keys in the dictionary using the dictionary’s keys()
function.
# create a dictionary employees = { "Jim": "Sales", "Dwight": "Sales", "Angela": "Accounting" } # get keys of dictionary print(employees.keys())
Output:
dict_keys(['Jim', 'Dwight', 'Angela'])
You can see that we get all the names of the employees (the keys in the dictionary employee
) in a dict_keys
object.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Now, you can also convert this object to a list using the Python built-in list()
function.
# dictionary keys as list print(list(employees.keys()))
Output:
['Jim', 'Dwight', 'Angela']
We now have the keys in the dictionary employees
as a list.
Using Iteration
Alternatively, you can iterate through the dictionary items and append the key in each iteration to a result list.
# create a dictionary employees = { "Jim": "Sales", "Dwight": "Sales", "Angela": "Accounting" } # get keys of dictionary keys_ls = [] for key, val in employees.items(): keys_ls.append(key) print(keys_ls)
Output:
['Jim', 'Dwight', 'Angela']
We get the keys in the dictionary as a list.
You can also reduce the above computation to a single line using list comprehension.
# get keys of dictionary keys_ls = [key for key, val in employees.items()] print(keys_ls)
Output:
['Jim', 'Dwight', 'Angela']
We get the same result as above.
In this tutorial, we looked at different ways to get all the keys in a Python dictionary. Using the dictionary’s keys()
function is a simpler and a direct way of getting the keys as compared to the iteration based methods.
You might also be interested in –
- Get Value for a Key in a Python Dictionary
- Python – Count Keys in a Dictionary
- Check If a Python Dictionary Contains a Specific Key
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.