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 –
- Define a variable to store a dictionary object with some key-value pairs
- Use the map() function with the lambda expression as the function to be applied on each element of the dictionary object
- 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.
- 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:
- Python | Asking the user for input until they give a valid response
- Python | How to iterate through two lists in parallel?
- Python | How to sort a dictionary by value?
- Python | Remove items from a list while iterating
- Python | How to get dictionary keys as a list
- How to represent Enum in Python?
- 5 methods | Flatten a list of lists | Convert nested list into a single list in Python
- What does if __name__ == __main__ do in Python?
- Python | CRUD operations in MongoDB
- Create your own ChatGPT with Python
- Filter List in Python | 10 methods
- Radha Krishna using Python Turtle
- Yield Keyword in Python
- Python Programming Examples | Fundamental Programs in Python
- Python | Delete object of a class
- Python | Modify properties of objects
- Python classmethod
- Python | Create a class that takes 2 parameters, print both parameters
- Python | Create a class, create an object of the class, access, and print property value
- Python | Calculator using lambda
- Python | Multiply numbers using lambda
- Python | Print Namaste using lambda
- Iterate over a string in Python
- Python | join() | Join list of strings
- Python | isalnum() method | check if a string consists only of alphabetical characters or numerical digits
- Python | isupper() and islower() methods to check if a string is in all uppercase or lowercase letters
- Python | count substring in a String | count() method
- Python | enumerate() on String | Get index and element
- Python | Convert a String to list using list() method
- Euler’s Number in Python