
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
Print All Keys of a Dictionary in Python
A Python dictionary is an unordered collection of data values. It contains a key-value pair, in contrast to other data structures that only include one value per entry. In this article, we are going to see 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 be printed using the print() function. This method returns a list object, which contains every key in the dictionary. The dictionary 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())
Following is an output of the above program-
dict_keys(['Novel', 'year', 'author', 'character'])
Using a 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 we 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)
Following is an output of the above example -
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 entire contents are then printed, i.e., all keys of the dictionary.
Example
Below 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)
Following is an output of the above program -
['Novel', 'character', 'author', 'year']
By creating a list comprehension
We can also use 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 -
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'} # Using list comprehension to print each key [print(key) for key in my_dict]
Following is an output of the above code -
Novel year author character
Using itemgetter Module
Python's operator module provides a function called itemgetter(), which can be used to retrieve specific items from objects such as dictionaries, lists, or tuples.
Example
Following is an example to print all the keys of a dictionary using itemgetter method -
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 program -
['Novel', 'character', 'author', 'year']