Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Get dictionary keys as a list

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 30 Dec, 2022
Improve Article
Save Article

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()

The dict.keys() method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary in order of insertion.

Python3




# Driver program
dict = {1: 'Geeks', 2: 'for', 3: 'geeks'}
print(dict.keys())


Output:

dict_keys([1, 2, 3])

Method 2: 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)


Output:

[1, 2, 3]

Method 3: 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))


Output:

[1, 2, 3]

Method 4:  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)


Output:

[1, 2, 3]

Method 5: Dictionary Keys to List using Unpacking with * 

Unpacking with * works with any object that is iterable 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']

Method 6: 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 then 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']

Method 7: 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


Output

[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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!