
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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. We can get the list of all the keys from a Python dictionary using the following methods -
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.
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.
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())
When you run the program, it will show this output -
dict_keys([10, 12, 14])
Using list() & dict.keys() function
The list() method in Python accepts any iterable as an argument and returns a list. Iterable is an object in Python that can be iterated over. And the dict.keys() method is used to get the keys of a dictionary.
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()))
After running the program, you will get this result -
[10, 12, 14]
Using List comprehension
We can use list comprehension to get the list of all the keys of a dictionary. List comprehension is a concise way to create lists in Python.
Example
The following program returns the list of all the keys of a dictionary using a 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])
This output will be displayed when the program runs -
[10, 12, 14]
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.
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])
You will see this result after executing the program -
[10, 12, 14]
Using append() function & For loop
We can use the for loop to traverse through all the keys of the dictionary using the keys() function. to get the list of keys, we need to append each key of the dictionary to a list using the append() function and print the list at the end.
Example
The following program prints 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
The program gives this output when it is run -
[10, 12, 14]