Get List of Keys from Dictionary in Python (4 Examples)

 

In this tutorial, you’ll learn how to get a list from your dictionary keys in Python.

The content of the tutorial is structured as follows:

Let’s go straight to the examples!

 

Create Sample Data

As the first step in this tutorial, we will create an example dictionary to use in the following examples.

my_dict = {'apple':1, 'pineapple': 5, 'watermelon': 3, 'banana': 4}
print(my_dict)
# {'apple': 1, 'pineapple': 5, 'watermelon': 3, 'banana': 4}

As you can see, we have created my_dict, a sample dictionary that contains four key-value pairs. Let’s see how to print the keys as a list!

 

Example 1: Return Dictionary Keys as List via keys() Method & list() Function

In this first example, we will use the keys() method to retrieve the keys in my_dict.

keys_list = list(my_dict.keys())
print(keys_list)
# ['apple', 'pineapple', 'watermelon', 'banana']

As shown in the previous Python output, the keys() method returns a view object of the dictionary’s keys, which we have converted into a list using the list() function.

 

Example 2: Return Dictionary Keys as List via for Loop

Example 2 shows how to return the dictionary keys as a list using a for loop.

keys_list = []
for key in my_dict:
    keys_list.append(key)
print(keys_list)
# ['apple', 'pineapple', 'watermelon', 'banana']

Notice that, first, we have defined an empty list called keys_list. Then, the for loop iterated over the keys in my_dict and appended each key to keys_list.

 

Example 3: Return Dictionary Keys as List via List Comprehension

This method shows how to use list comprehension to create a new list that contains our dictionary’s keys.

keys_list = [key for key in my_dict]
print(keys_list)
# ['apple', 'pineapple', 'watermelon', 'banana']

Here, we have used list comprehension to create a new list named keys_list, which contains all the keys in my_dict.

 

Example 4: Return Dictionary Keys as List via * Operator

This last method is only available in Python 3.5 and later. It uses the * operator to unpack the keys of the dictionary to a new list as follows.

keys_list = [*my_dict]
print(keys_list)
# ['apple', 'pineapple', 'watermelon', 'banana']

As shown, the [*my_dict] expression provides the same output as in the previous examples.

 

Video, Further Resources & Summary

Do you need more explanations on how to print a dictionary’s keys as a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out some other tutorials on Statistics Globe:

This post has shown how to get a list of keys from a dictionary in Python language. In case you have further questions, you may leave a comment below.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get further information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top