- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 print all the keys of a dictionary in Python
An unordered collection of data values is what a Python dictionary is. A Python dictionary contains a key: value pair, in contrast to other data structures that only include one value per entry. This article explains about the various ways to print all the keys of a dictionary in Python.
Using dict.keys() Method
Python's dict.keys() method can be used to retrieve the dictionary keys, which can then be printed using the print() function. A view object that shows a list of every key in the dictionary is the result of the dict.keys() method.
The dictionary's elements can be accessed using the dict.keys() method, just like we do with a list by index.
Example
Following is an example to print all the keys of a dictionary using dict.keys() method −
dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } print(dictionary.keys())
Output
Following is an output of the above code −
['Novel', 'character', 'author', 'year']
Using dictionary.items() Method
The built-in Python method items() is used to retrieve all the keys and corresponding values. We can print the dictionary's keys and values by combining the items() method with a for loop.
This method is more practical if you wish to print keys one at a time.
Example
Following is an example to print all the keys of a dictionary using dictionary.items() method −
dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } for keys, value in dictionary.items(): print(keys)
Output
Following is an output of the above code −
Novel character author year
By creating a list of all keys
From the iterable sequence given by the dict.keys() function, we can also generate a list of keys. The list's entire contents are then printed (all keys of the dictionary).
Example
Following is an example to print all the keys of a dictionary by creating a list of all keys −
dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } # Getting all the keys of a dictionary as a list list_of_the_keys = list(dictionary.keys()) # Printing the list which contains all the keys of a dictionary print(list_of_the_keys)
Output
Following is an output of the above code.
['Novel', 'character', 'author', 'year']
By creating a list comprehension
We can also use this list comprehension to repeatedly print each key in the dictionary by iterating over all the keys.
Example
Following is an example to print all the keys of a dictionary by creating a list comprehension −
dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } # Iterating over all the keys of a dictionary and printing them one by one [print(keys) for keys in dictionary]
Output
Following is an output of the above code −
Novel year author character
Using itemgetter Module
The itemgetter from the operator module returns a callable object that uses the operand's __getitem__() method to retrieve an item from it. The method is then typecast to list after being mapped to dict.items().
Example
Following is an example to print all the keys of a dictionary using itemgetter −
from operator import itemgetter def List(dictionary): return list(map(itemgetter(0), dictionary.items())) dictionary = { 'Novel': 'Pride and Prejudice','year': '1813','author': 'Jane Austen','character': 'Elizabeth Bennet'} print(List(dictionary))
Output
Following is an output of the above code.
['Novel', 'character', 'author', 'year']