Get Dictionary Keys as List in Python

In this article, we will dicsuss different ways to get the dictionary keys as List in Python.

Table Of Contents

Introduction

Suppose we have a dictionary like this,

# A Dictionary containing student details
student = { 'Name' : 'Ritika',
            'Age' : 27,
            'City' : 'Delhi',
            'Country' : 'India'}

It contains a student infromation like Name, Age, City, and Country. We want to create a list of all keys in this dictionary. Like this,

['Name', 'Age', 'City', 'Country']

There are different ways to do this. Let’s discuss them one by one.

Method 1: Using dict.keys()

In Python, the dict class provides a function keys(). It returns a view on dictionary keys. Then we can pass it to the list constructor, and it will return a list of dictionary keys. Let’s see an example,

# A Dictionary containing student details
student = { 'Name' : 'Ritika',
            'Age' : 27,
            'City' : 'Delhi',
            'Country' : 'India'}

# Get keys of dictionary as List
listOfkeys = list(student.keys())

# print the conetnts of list
print(listOfkeys)

# print the type
print(type(listOfkeys))

Output:

['Name', 'Age', 'City', 'Country']
<class 'list'>

The keys() method of dictionary returned all keys, and we created a list of all keys. We also printed the type of list object, just to make it confirm, that we have a list only.

Method 2: Using For Loop

Create a new empty list. Iterate over all keys of dictionary, and add them to new list, one by one. We can iterate over all keys of dictionary by applying a for loop on the dictionary object only. Let’s see an example,

# A Dictionary containing student details
student = { 'Name' : 'Ritika',
            'Age' : 27,
            'City' : 'Delhi',
            'Country' : 'India'}

# Create empty List
listOfkeys = []

# Get keys of dictionary as List
for key in student:
    listOfkeys.append(key)

# print the conetnts of list
print(listOfkeys)

# print the type
print(type(listOfkeys))

Output:

['Name', 'Age', 'City', 'Country']
<class 'list'>

It gave us a list of all keys in the dictionary. We also printed the type of list object, just to make it confirm, that we have a list only.

Method 3: Using List Comprehension

This solution is similar to the previous one, but with one difference only. Instead of a direct for loop, we will use the List Comprehension, to iterate over all keys in dictionary, and appending them to a new list. Let’s see an example,

# A Dictionary containing student details
student = { 'Name' : 'Ritika',
            'Age' : 27,
            'City' : 'Delhi',
            'Country' : 'India'}


# Get keys of dictionary as List
listOfkeys = [key for key in student]

# print the conetnts of list
print(listOfkeys)

# print the type
print(type(listOfkeys))

Output:

['Name', 'Age', 'City', 'Country']
<class 'list'>

It gave us a list of all keys in the dictionary. We also printed the type of list object, just to make it confirm, that we have a list only.

Method 4: Using Unpacking

We can also unpack a dictionary object to a sequence of keys only, by applying the astrik to dictionary object. Then we can encapsulate this sequence of keys, into a list object. Let’s see an example,

# A Dictionary containing student details
student = { 'Name' : 'Ritika',
            'Age' : 27,
            'City' : 'Delhi',
            'Country' : 'India'}


# Get keys of dictionary as List
listOfkeys = [*student]

# print the conetnts of list
print(listOfkeys)

# print the type
print(type(listOfkeys))

Output:

['Name', 'Age', 'City', 'Country']
<class 'list'>

It gave us a list of all keys in the dictionary. We also printed the type of list object, just to make it confirm, that we have a list only.

Method 5: Using map() and itemgetter()

Get a sequence of all key-value pairs of dictionary. Then select the key from each pair, and append to a list.

To implement this, follow these steps,

  • Get a sequence of key-value pairs of dictionary using the items() function.
  • Pass the sequence of pairs, and itemgetter(0) function to the map() method.
  • The map() method will apply the argument itemgetter(0), on each pair in the sequence. Basically for each pair, it selects the key from pair.
  • Create a list from values returned by the map() function. It will be the list of keys.

Let’s see an example,

from operator import itemgetter

# A Dictionary containing student details
student = { 'Name' : 'Ritika',
            'Age' : 27,
            'City' : 'Delhi',
            'Country' : 'India'}


# Get keys of dictionary as List
listOfkeys = list(map(itemgetter(0), student.items()))

# print the conetnts of list
print(listOfkeys)

# print the type
print(type(listOfkeys))

Output:

['Name', 'Age', 'City', 'Country']
<class 'list'>

It gave us a list of all keys in the dictionary. We also printed the type of list object, just to make it confirm, that we have a list only.

Summary

We learned about different ways to get dictionary keys as list in Python. Thanks.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top