Get keys of a python dictionary

Get Keys of a Python Dictionary – With Examples

In this tutorial, we will look at how to get the keys of a Python dictionary with the help of some examples.

Get keys of a python dictionary

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.

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top