How to Print Dictionary Keys in Python
Last Updated :
08 Feb, 2024
Dictionaries in Python are versatile data structures that allow you to store and manipulate key-value pairs. If you want to print just the keys of a dictionary, Python provides several ways to achieve this. In this article, we’ll explore different methods to print dictionary keys in Python.
What is Keys in Dictionary?
In Python, a dictionary is a collection of key-value pairs. Each key in a dictionary must be unique, and is associated with a specific value. The keys serve as identifiers, allowing you to access the corresponding values efficiently.
Here’s an example of a dictionary with keys and values:
Input : person_info = {
'name': 'John',
'age': 30,
'city': 'New York',
'occupation': 'Engineer'
}
Output: Name: John
Age: 30
City: New York
Occupation: Engineer
How to Print Dictionary Keys in Python?
Below, are the methods of How to Print Dictionary Keys in Python.
Print Dictionary Keys in Python Using keys()
Method
The keys()
method returns a view object that displays a list of all the keys in a dictionary. You can convert this view object to a list or iterate over it directly to print the keys. In this example, we first convert the keys view object to a list using list()
. You can also iterate directly over the keys view without converting it to a list.
Python3
sample_dict = { 'name' : 'John' , 'age' : 25 , 'city' : 'New York' }
keys_list = list (sample_dict.keys())
print ( "Method 1 - Using keys() method:" )
print (keys_list)
|
Output
Method 1 - Using keys() method:
['name', 'age', 'city']
Print Dictionary Keys in Python Iterating Through Dictionary
You 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.
Python3
sample_dict = { 'name' : 'John' , 'age' : 25 , 'city' : 'New York' }
print ( "Method 2 - Iterating through the dictionary:" )
for key in sample_dict:
print (key)
|
Output
Method 2 - Iterating through the dictionary:
name
age
city
Print Dictionary Keys in Python Using List Comprehension
List comprehension is a concise way to create lists in Python. You can use list comprehension to create a list of keys. List comprehension provides a more compact syntax for creating lists, and it can be a convenient way to extract keys from a dictionary.
Python3
sample_dict = { 'name' : 'John' , 'age' : 25 , 'city' : 'New York' }
keys_list = [key for key in sample_dict]
print ( "Method 3 - Using list comprehension:" )
print (keys_list)
|
Output
Method 3 - Using list comprehension:
['name', 'age', 'city']
Conclusion
Printing dictionary keys in Python is a common operation when working with dictionaries. Depending on your preference and specific use case, you can choose the method that best fits your needs. Whether it’s using the keys()
method, iterating through the dictionary, employing list comprehension, or working directly with the dict_keys
type, Python offers multiple approaches to achieve the desired result
Share your thoughts in the comments
Please Login to comment...