In Python, dictionaries are unordered collections of key-value pairs, where each key is mapped to a specific value. When you are working with dictionaries, it is often necessary to retrieve/print a list of all the keys stored in the dictionary.
To print all the keys of a dictionary in Python, you can use the keys()
method of the dictionary. The dict.keys() method returns an iterable of the type dict_keys
which contains all the keys present in the dictionary.
The keys returned by the dict.keys() method are in the same order as they were added to the dictionary. You can then run a for
loop over the keys list to print all the keys or use it according to your needs.
The following example shows how you can print all the keys of a dictionary in Python:
# Create a dictionary person = { 'name':'John Doe', 'age': 23, 'gender': 'Male', 'marks':'89.5%' } # Get all keys keys = list(person.keys()) # Print keys list print(keys) # Print each key in the keys list for key in keys: print(key)
Output:
['name', 'age', 'gender', 'marks'] name age gender marks
Method 2: Use the dict.items() Method
The dict.items() method can also be used to print all the keys of a dictionary. The items()
method returns an iterable of the type dict_items
which contains all the key-value pairs present in the dictionary.
Each key-value pair in the iterable returned by the dict.items() method is an immutable tuple of two items, where the first item represents the key and the second item is the value of the corresponding key.
The following example prints all the keys of the dictionary using the dict.items() method:
# Create a dictionary person = { 'name':'John Doe', 'age': 23, 'gender': 'Male', 'marks':'89.5%' } # Get all key-value pairs keyValPairs = list(person.items()) # Print key-value pairs print(keyValPairs) # Print each key of the dictionary for key,val in keyValPairs: print(key)
Output:
[('name', 'John Doe'), ('age', 23), ('gender', 'Male'), ('marks', '89.5%')] name age gender marks
Method 3: By Converting the Dictionary into a List
A Python dictionary can very easily be converted into a list using the built-in list()
constructor. The list()
constructor takes a single parameter which can be any iterable object such as a string, tuple, list, dictionary, etc. and converts it into a list.
When you pass a dictionary into the list()
constructor as a parameter, it returns a new list which contains all the keys of the dictionary. The order of the keys in the new list remains the same as their insertion order.
See the following example:
# Create a dictionary person = { 'name':'John Doe', 'age': 23, 'gender': 'Male', 'marks':'89.5%' } # Convert dictionary into a list of keys keys = list(person) # Print the keys list print(keys) # Print each key of the dictionary for key in keys: print(key)
Output:
['name', 'age', 'gender', 'marks'] name age gender marks
Method 4: Using a for…in Loop
You can also directly run a for
loop over the given dictionary to print all of its keys.
When you run a for
loop over a dictionary, it one by one gets each key of the dictionary in the same order as they were inserted during its creation.
See the following example:
# Create a dictionary person = { 'name':'John Doe', 'age': 23, 'gender': 'Male', 'marks':'89.5%' } #Loop through the dictionary for key in person: print(key)
Output:
name age gender marks
Conclusion
In this article, we learned the different ways of printing all the keys of a dictionary in Python.
In summary, an straightforward way to print all the keys of a dictionary in Python is to use the dict.keys() method. The dict.keys() method returns a new list that contains all the keys of the dictionary in the same order as their insertion.
You can also use other approaches such as dict.items() method, the list() constructor, a for…in loop,etc. based on your requirements.
Thanks for reading.