Given a dictionary, write a Python program to get the dictionary keys as a list.
Examples:
Input : {1:'a', 2:'b', 3:'c'} Output : [1, 2, 3] Input : {'A' : 'ant', 'B' : 'ball'} Output : ['A', 'B']
Method 1: Get dictionary keys as a list using dict.keys()
Python list() function takes any iterable as a parameter and returns a list. In Python, iterable is the object you can iterate over.
Python3
mydict = { 1 : 'Geeks' , 2 : 'for' , 3 : 'geeks' } keysList = list (mydict.keys()) print (keysList) |
[1, 2, 3]
The time complexity of the program is O(n), where n is the number of keys in the dictionary.
The space complexity of the program is O(n), where n is the number of keys in the dictionary. This is because the program creates a new list object with the same number of elements as the keys in the dictionary, which requires additional memory.
Method 2: Get dictionary keys as a list using For Loop and append method
In this method, we will iterate over each key using the dict.keys() function and append them to a new list named as a list.
Python3
# Python program to get # dictionary keys as list def getList( dict ): list = [] for key in dict .keys(): list .append(key) return list # Driver program dict = { 1 : 'Geeks' , 2 : 'for' , 3 : 'geeks' } print (getList( dict )) |
[1, 2, 3]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Dictionary Keys to List using List Comprehension
Here, we will try to shorten our code using list comprehension in Python.
Python3
dict = { 1 : 'Geeks' , 2 : 'for' , 3 : 'geeks' } keysList = [key for key in dict ] print (keysList) |
[1, 2, 3]
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.
Method 4: Dictionary Keys to List using Unpacking with *
Unpacking with * works with any iterable object, and since dictionaries return their keys when iterated through, you can easily create a list by using it within a list literal.
Python3
def getList( dict ): return [ * dict ] # Driver program dict = { 'a' : 'Geeks' , 'b' : 'For' , 'c' : 'geeks' } print (getList( dict )) |
Output:
['a', 'b', 'c']
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 5: Dictionary Keys to List using itemgetter
The itemgetter from the operator module returns a callable object that fetches an item from its operand using the operand’s __getitem__() method. This method is mapped to dict.items() and then typecasted to list.
Python3
from operator import itemgetter def getList( dict ): return list ( map (itemgetter( 0 ), dict .items())) # Driver program dict = { 'a' : 'Geeks' , 'b' : 'For' , 'c' : 'geeks' } print (getList( dict )) |
Output:
['a', 'b', 'c']
Method6: Using Map and lambda
Another approach to get the dictionary keys as a list is to use the map() function in combination with a lambda function.
Here is an example of how this can be done:
Python3
def get_keys_as_list(dictionary): return list ( map ( lambda x: x[ 0 ], dictionary.items())) # Driver program dictionary = { 1 : 'Geeks' , 2 : 'for' , 3 : 'geeks' } print (get_keys_as_list(dictionary)) #This code is contributed by Edula Vinay Kumar Reddy |
[1, 2, 3]
This approach uses the map() function to apply the lambda function to each item in the dictionary, which returns the key from each item. The resulting iterator object is then passed to the list() function to create a list of the keys.
This approach has a time complexity of O(n) and an auxiliary space complexity of O(n), where n is the number of keys in the dictionary. It is a concise and efficient way to get the dictionary keys as a list.