Open In App

How to Print Dictionary Keys in Python

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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)

Output
name
age
city

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.


Next Article
Practice Tags :

Similar Reads