How to Get Keys of a Dictionary in Python?

As a Python developer working on a project for a US-based company, I recently faced an issue where I needed to extract all the keys from a dictionary and store them in a list. In this tutorial, I will explain how to get keys of a dictionary in Python. After researching and experimenting with different methods, I discovered several ways to accomplish this task effectively.

Get Keys of a Dictionary in Python

Before getting into the solutions, let’s briefly discuss what dictionaries are in Python. A dictionary is an unordered collection of key-value pairs, where each key is unique. It allows you to store and retrieve values based on their associated keys. Python Dictionaries are defined using curly braces {}, and the key-value pairs are separated by commas.

Here’s an example of a dictionary containing information about US states and their capitals:

state_capitals = {
    "California": "Sacramento",
    "New York": "Albany",
    "Texas": "Austin",
    "Florida": "Tallahassee"
}

Read Write a Python Program to Remove Duplicates From a Dictionary

Method 1: Use the keys() Method

The most simple way to get the keys of a dictionary in Python is by using the built-in keys() method. This method returns a view object containing all the dictionary keys.

Here’s an example:

state_capitals = {
    "California": "Sacramento",
    "New York": "Albany",
    "Texas": "Austin",
    "Florida": "Tallahassee"
}

keys = state_capitals.keys()
print(keys)  

Output:

dict_keys(['California', 'New York', 'Texas', 'Florida'])

I executed the above example code and added the screenshot below.

Get Keys of a Dictionary in Python

To convert the view object into a list, you can simply pass it to the list() function:

keys_list = list(state_capitals.keys())
print(keys_list)  

Output:

['California', 'New York', 'Texas', 'Florida']

I executed the above example code and added the screenshot below.

How to Get Keys of a Dictionary in Python

Check out How to Create a Dictionary in Python Using a For Loop?

Method 2: Use a for Loop

Another way to get the keys of a dictionary is by iterating over the dictionary using a for loop in Python. This method allows you to access each key directly without the need for a separate function call.

Here’s an example:

state_capitals = {
    "California": "Sacramento",
    "New York": "Albany", 
    "Texas": "Austin",
    "Florida": "Tallahassee"
}

keys_list = []
for key in state_capitals:
    keys_list.append(key)

print(keys_list) 

Output:

['California', 'New York', 'Texas', 'Florida']

I executed the above example code and added the screenshot below.

Get Keys of a Dictionary in Python for Loop

In this approach, we initialize an empty list called keys_list and then iterate over the dictionary using a for loop. In each iteration, the key variable represents the current key, which we append to the keys_list.

Read How to Convert Two Lists into a Dictionary in Python Without Using a built-in Method

Method 3: Use a List Comprehension

Python offers a concise way to create lists using list comprehensions. We can leverage this feature to extract the keys of a dictionary in a single line of code.

Here’s an example:

state_capitals = {
    "California": "Sacramento",
    "New York": "Albany",
    "Texas": "Austin", 
    "Florida": "Tallahassee"
}

keys_list = [key for key in state_capitals]
print(keys_list)

Output:

['California', 'New York', 'Texas', 'Florida']

I executed the above example code and added the screenshot below.

Get Keys of a Dictionary in Python List Comprehension

In this method, we create a list comprehension that iterates over the dictionary and collects all the keys into a new list called keys_list.

Read How to Create a Dictionary from a List in Python?

Conclusion

In this tutorial, I helped you to learn how to get keys of a dictionary in Python. I discussed three important methods to accomplish this task such as using the keys() method, using a for loop, and using a list comprehension.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.