How to get a list of all the keys from a Python dictionary?


In this article, we will show you how to get a list of all the keys from a Python dictionary using various methods. We can get the list of all the keys from a Python dictionary using the following methods −

  • Using dict.keys() method

  • Using list() & dict.keys() function

  • Using List comprehension

  • Using the Unpacking operator(*)

  • Using append() function & For loop

Assume we have taken an example dictionary. We will return the list of all the keys from a Python dictionary using different methods as specified above.

Method 1: Using dict.keys() method

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

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input dictionary and add some random key-value pairs to it.

  • Use the keys() function and apply it to the input dictionary to get the list of all the keys of a dictionary and print it.

Example

The following program returns the list of all the keys of a dictionary using the keys() function −

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Printing the list of keys of a dictionary using keys() function print(demoDictionary.keys())

Output

On executing, the above program will generate the following output −

dict_keys([10, 12, 14])

Method 2: Using list() & dict.keys() function

The list() method in Python accepts any iterable as an argument and returns a list. Iterable is the object in Python that can be iterated over.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input dictionary

  • Print the list of all the keys of a dictionary with the keys() function (the dict.keys() method provides a view object that displays a list of all the keys in the dictionary in order of insertion) by applying it to the input dictionary and convert the result to a list using the list() function (converts the sequence/iterable to a list).

Example

The following program returns the list of all the keys of a dictionary using the list() and keys() functions -

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Printing the list of keys of a dictionary using keys() function # list() methods convert an iterable into a list print(list(demoDictionary.keys()))

Output

[10, 12, 14]

Method 3: Using List comprehension

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input dictionary.

  • Use the keys() function and apply it to the input dictionary to get the list of all the keys of a dictionary.

  • Print the list of keys of a dictionary by traversing through each key in the above keys list using the list comprehension and for loop.

Example

The following program returns the list of all the keys of a dictionary using the List comprehension−

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # getting all the keys from a dictionary dictionaryKeys = demoDictionary.keys() # Printing the list of keys of a dictionary by traversing through each key # in the above keys list using the list comprehension print([key for key in dictionaryKeys])

Output

[10, 12, 14]

Method 4: Using the Unpacking operator(*)

The unpacking operator * works with any iterable object, and because dictionaries give their keys when iterated, you can easily generate a list by using it within a list literal.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input dictionary

  • Print the list of all the keys of a dictionary by using the unpacking operator (*) and below syntax (Here it will unpack all the keys of the dictionary and return as list using * as the unpacking operator)

print([*demoDictionary])

Example

The following program returns the list of all the keys of a dictionary using the unpacking operator(*) −

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Printing the list of keys of a dictionary by using the unpacking operator(*) print([*demoDictionary])

Output

[10, 12, 14]

Method 5: Using append() function & For loop

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input dictionary

  • Create an empty list to store all the keys of an input dictionary

  • Use the for loop, to traverse through all the keys of the dictionary using the keys() function.

  • Append each key of the dictionary to the list using the append() function (adds the element to the list at the end) by passing the corresponding key as an argument to it.

  • Print the list of all the keys of a dictionary.

Example

The following program returns the list of all the keys of a dictionary using the append() function & for loop −

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # an empty list for storing dictionary keys dictKeysList = [] # Traversing through all the keys of the dictionary for dictKey in demoDictionary.keys(): # appending each key to the list dictKeysList.append(dictKey) # Printing the list of keys of a dictionary print(dictKeysList)

Output

On executing, the above program will generate the following output −

[10, 12, 14]

Conclusion

This article taught us how to use the keys() function to obtain the entire dictionary's keys as well as how to use the list() function to turn the keys into a list. Additionally, we learned how to use the list comprehension and for loop in the same code to transform the keys from the dictionary returned by the keys() method to a list. Finally, we learned how to add elements to a list using the append() function (Here we added keys to the list).

Updated on: 25-Aug-2023

38K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements