Python | How to get dictionary keys as a list

How to get dictionary keys as a list in Python?

In this article, you will learn 6 different methods to get dictionary keys as a list in python. These methods include various Python programming concepts like list comprehension, map() function, unpacking operator and much more, which will help you strengthen your basic python concepts. So, Let’s start with the first method.

Before I present you all 6 different methods to get dictionary keys as a list in python, I highly recommend you must complete the following Python concepts:

Method 1 – Using the keys() method

We can use the Python dictionary’s keys() method which returns a view object containing dictionary’s keys and pass it to a list() function to get a list and can be used for dictionary keys to list conversion.

d = {'python': 1, 'java': 2, 'c++': 3}
res = list(d.keys())
print("The list is:", res)

Output:

The list is: ['python', 'java', 'c++']

Method 2 – Using for loop with append()

We can run a simple loop to iterate over the dictionary object and get each key and append it to a new list. This method will help you to get dictionary keys as a list in python.

d = {'python': 1, 'java': 2, 'c++': 3}
res = []
for i in d:
    res.append(i)
print("The list is:", res)

Output:

The list is: ['python', 'java', 'c++']

Method 3 – Using list comprehension

List comprehension is a way to create a new list from the values of a given iterable such as list. It provides a simple and short syntax to create a list in Python. We can use list comprehension to iterate over the keys of the dictionary object to Convert dictionary keys to list in Python.

d = {'python': 1, 'java': 2, 'c++': 3}
res = [key for key in d]
print("The list is:", res)

Output:

The list is: ['python', 'java', 'c++']

Method 4 – Using map() function with lambda expression

The map() function in Python is a function that can take two arguments, a function and an iterable like list or dictionary. It applies the given function which is passed as an argument, to all the elements of the iterable such as a list or dictionary and finally returns the answer list. We can use the map() function with a lambda expression to get the keys of a dictionary object and then ultimately to dictionary keys as list in python. The algorithmic steps are as follows –

  1. Define a variable to store a dictionary object with some key-value pairs
  2. Use the map() function with the lambda expression as the function to be applied on each element of the dictionary object
  3. The returned value from the map() function is a map object so to get a list out of this object, pass it through list() constructor.
  4. Store the resulting list and print dictionary key list generation.
d = {'python': 1, 'java': 2, 'c++': 3}
res = list(map(lambda x: x, d))
print("The key list is:", res)

Output:

The key list is: ['python', 'java', 'c++']

Method 5 – Using unpacking operator(*)

In this method of extracting dictionary keys as list in Python, the (*) operator is known as an unpacking operator which can be used to get the values from a list, tuple or dictionary as individual elements. We will use the (*) unpacking operator to get the keys as a list from the dictionary object. Use the unpacking operator(*) with the dictionary object and pass it in a list constructor to get the list.

d = {'python': 1, 'java': 2, 'c++': 3}
res = [*d]
print("The list is:", res)

Output:

The list is: ['python', 'java', 'c++']

Method 6 – Using list() with dictionary as parameter

The list() constructor returns a list of object that is passed inside it. We can use the list constructor list() and pass the dictionary object as an argument in it for retrieving dictionary keys as a list in Python.

d= {'python': 1, 'java': 2, 'c++': 3}
res = list(d)
print("The list is:", res)

Output:

The list is: ['python', 'java', 'c++']

Conclusion

In this article, I have tried to cover different ways of how you can get dictionary keys as a list. It’s always better to know more than one way of doing something in programming, as this can help you understand the advantages and disadvantages of doing something in a particular way. Here, we have covered some very important fundamental concepts of Python programming like – using list comprehension, map() function, unpacking operator(*) and more. I hope this was helpful and you could learn something valuable from this article.

Thank you for visiting our website.


Also read:

Share:

Author: Ayush Purawr