Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to print all the keys of a dictionary in Python
A Python dictionary is an unordered collection of data values that stores key-value pairs. In this article, we will explore various methods to print all the keys of a dictionary in Python.
Using dict.keys() Method
The dict.keys() method returns a dictionary view object containing all the keys. This is the most direct way to access all keys in a dictionary ?
Example
dictionary = {
'Novel': 'Pride and Prejudice',
'year': '1813',
'author': 'Jane Austen',
'character': 'Elizabeth Bennet'
}
print(dictionary.keys())
The output of the above code is ?
dict_keys(['Novel', 'year', 'author', 'character'])
Using dictionary.items() Method
The items() method returns key-value pairs. We can iterate through these pairs and print only the keys ?
Example
dictionary = {
'Novel': 'Pride and Prejudice',
'year': '1813',
'author': 'Jane Austen',
'character': 'Elizabeth Bennet'
}
for key, value in dictionary.items():
print(key)
The output of the above code is ?
Novel year author character
Converting Keys to List
You can convert the dictionary keys to a list using the list() function. This creates a permanent list of all keys ?
Example
dictionary = {
'Novel': 'Pride and Prejudice',
'year': '1813',
'author': 'Jane Austen',
'character': 'Elizabeth Bennet'
}
# Converting keys to a list
keys_list = list(dictionary.keys())
print(keys_list)
The output of the above code is ?
['Novel', 'year', 'author', 'character']
Using List Comprehension
List comprehension provides a concise way to iterate through dictionary keys and print each one individually ?
Example
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
# Using list comprehension to print each key
[print(key) for key in my_dict]
The output of the above code is ?
name age city
Using itemgetter from operator Module
The itemgetter() function from the operator module can extract the first element (key) from each key-value pair ?
Example
from operator import itemgetter
def get_keys(dictionary):
return list(map(itemgetter(0), dictionary.items()))
dictionary = {
'Novel': 'Pride and Prejudice',
'year': '1813',
'author': 'Jane Austen',
'character': 'Elizabeth Bennet'
}
print(get_keys(dictionary))
The output of the above code is ?
['Novel', 'year', 'author', 'character']
Comparison
| Method | Returns | Best For |
|---|---|---|
dict.keys() |
dict_keys object | Simple key access |
items() + loop |
Individual keys | Processing keys one by one |
list(keys()) |
List of keys | When you need indexing |
| List comprehension | Individual keys | Concise iteration |
Conclusion
The dict.keys() method is the most straightforward approach for accessing dictionary keys. Use list(dict.keys()) when you need the keys as a list, and choose iteration methods when processing keys individually.
