How to Print Dictionary Keys in Python
Last Updated :
26 Apr, 2025
We are given a dictionary and our task is to print its keys, this can be helpful when we want to access or display only the key part of each key-value pair. For example, if we have a dictionary like this: {'gfg': 1, 'is': 2, 'best': 3} then the output will be ['gfg', 'is', 'best']. Below, are the methods of How to Print Dictionary Keys in Python.
- Using keys() Method
- Iterating Through Dictionary
- Using List Comprehension
Using keys() Method
This method uses the keys() function to get a view object of the dictionary’s keys and then converts it into a list.
Python
d = {'name': 'John', 'age': 25, 'city': 'New York'}
a = list(d.keys())
print(a)
Output['name', 'age', 'city']
Explanation: The keys() method returns a view object that displays a list of all the keys in the dictionary. Using list() converts the view object into a list, which is then printed
Iterating Through Dictionary
We can iterate through the dictionary directly using a for loop and print each key individually. This method is straightforward and concise, making it a popular choice for printing dictionary keys.
Python
d = {'name': 'John', 'age': 25, 'city': 'New York'}
for key in d:
print(key)
Explanation: This method directly iterates over the dictionary. By default, the for loop iterates through the dictionary’s keys, printing each key in the iteration.
Using List Comprehension
List comprehension is used here to create a list of dictionary keys in a single, compact expression.
Python
d = {'name': 'John', 'age': 25, 'city': 'New York'}
a = [key for key in d]
print(a)
Output['name', 'age', 'city']
Explanation: List comprehension creates a new list by iterating over the dictionary and collecting its keys. This method is more compact and functional in nature. The resulting list is then printed.
Similar Reads
How to Print a Dictionary in Python Python Dictionaries are the form of data structures that allow us to store and retrieve the key-value pairs properly. While working with dictionaries, it is important to print the contents of the dictionary for analysis or debugging.Example: Using print FunctionPython# input dictionary input_dict =
3 min read
How to Initialize Dictionary in Python In Python dictionary, each key is unique and we can store different data types as values. The simplest and most efficient method to initialize a dictionary is using curly braces {}. This is the easiest and most common way to create a dictionary. We can use curly braces '{} ' and separate the keys an
2 min read
Get Total Keys in Dictionary - Python We are given a dictionary and our task is to count the total number of keys in it. For example, consider the dictionary: data = {"a": 1, "b": 2, "c": 3, "d": 4} then the output will be 4 as the total number of keys in this dictionary is 4.Using len() with dictThe simplest way to count the total numb
2 min read
Dictionary keys as a list in Python In Python, we will encounter some situations where we need to extract the keys from a dictionary as a list. In this article, we will explore various easy and efficient methods to achieve this.Using list() The simplest and most efficient way to convert dictionary keys to lists is by using a built-in
2 min read
Python - How to Iterate over nested dictionary ? In this article, we will discuss how to iterate over a nested dictionary in Python. Nested dictionary means dictionary inside a dictionary and we are going to see every possible way of iterating over such a data structure. Nested dictionary in use: {'Student 1': {'Name': 'Bobby', 'Id': 1, 'Age': 20}
3 min read