How to Return Dictionary Keys as a List in Python – Detailed Guide?

Python dictionary allows you to store values as key-value pairs.

You can return dictionary keys as a list using list(yourdict) in Python.

This tutorial teaches you how to return dictionary keys as a list using different methods and when it is appropriate to use them.

Using Dict Object and List()

In this section, you’ll use the dictionary object and the list() constructor to return dictionary keys as a list.

  • Create a dictionary and pass the dictionary object to the list constructor.
  • All the keys in the dictionary will be added to the list and displayed.

Code

The following code demonstrates how to return dictionary keys as a list using the list() constructor.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

list(yourdict)

Output

The keys in the dictionary are converted into a list and displayed.

    ['one', 'two', 'three', 'four']

Using Dict.Keys()

dict.keys() method works differently in Python 3 and Python 2.

In Python 3, dict.keys() returns a new view of the dictionary keys. Any changes in the dictionary keys are reflected in this view.

In Python 2, dict.keys() returns a copy of the dictionary’s list of keys. It’s a copy. Hence any change in the dictionary keys is not reflected in this copy.

Since it returns different values, let us see how to use this method to return dictionary keys as a list in Python 3 and Python 2.

Return Dictionary Keys as a List in Python 3

In Python 3, you can pass the dict.keys() to the list constructor to return keys as a list.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

list(yourdict.keys())

Output

All the keys are converted into a list and displayed using the list() constructor and dict.keys() method.

    ['one', 'two', 'three', 'four']

Return Dictionary Keys as a List in Python 2

In Python 2, you can directly use the dict.keys() method to return keys as a list because this method returns a copy of the dictionary’s list of keys.

Code

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

yourdict.keys()

Output

The dict.keys() method has returned the list of keys of a dictionary.

['one', 'two', 'three', 'four']

Unpack with *

You can also unpack the dictionary keys as a list using the * operator. This functionality was proposed in the PEP-448.

This is supported from Python version 3.5. So you can use this method if you’re using Python version 3.5 or a later version.

Checking Python Version

To check which version of Python you are using, use the below code snippet.

Code*

import sys
print(sys.version)

Output

3.8.2 (default, Sep  4 2020, 00:03:40) [MSC v.1916 32 bit (Intel)]

Now you know which version of Python you are using.

You can use the below methods appropriately.

Getting Keys as List using * operator

You need to prefix the * operator with the dictionary name and pass it to the [] square brackets. You’ll see the keys extracted as a list.

Code

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

[*yourdict]

Output

['one', 'two', 'three', 'four']

This is how the unpacking operator can be used to return dictionary keys as a list in Python.

Additional Resources

Leave a Comment