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.

Advertisements

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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

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