“Dictionaries” are extremely environment friendly Python knowledge buildings that retailer/maintain knowledge in key-value pair format. The “key” within the Python dictionary is a sort of immutable knowledge that permits storing and accessing the “values”. However typically, we have to print out a dictionary’s keys, both for debugging functions or to control them.
The aim of this write-up is to display the approaches to print the dictionary keys in Python utilizing completely different strategies.
Find out how to Print Dict Keys in Python?
The next strategies are utilized to print the keys of a specific Python dictionary:
Technique 1: Print Dict Keys Using the “dict.keys()” Technique
In Python, the “dict.keys()” technique retrieves a view object containing the keys of a dictionary. A dictionary “key” could be accessed and modified using this technique.
Syntax
Instance
This code instance is used to print the dictionary “keys”:
dict_value = {‘Title’: ‘Anna’, ‘ID’: 1840, ‘Age’: 22}
print(‘Given Dictionary: ‘, dict_value)
print(dict_value.keys())
Within the above code, the “dict.keys()” technique is used with dot notation syntax to retrieve the view object containing the “keys” of the initialized dictionary.
Output
As seen, the keys of the dictionary have been displayed efficiently.
Technique 2: Print Dict Keys Using the “dictionary.gadgets()” Technique
The “dictionary.gadgets()” technique retrieves a view object containing the “key-value” pairs of a dictionary. This technique accesses each the keys and values of a dictionary collectively as tuples. The “for” loop could be utilized along with the “dict.gadgets()” technique to print solely the keys within the dictionary:
Syntax
Instance
The under code snippet is utilized to print the dictionary “keys”:
dict_value = {‘Title’: ‘Anna’, ‘ID’: 1840, ‘Age’: 22}
print(‘Given Dictionary: ‘, dict_value)
for keys, worth in dict_value.gadgets():
print(keys)
Based on the above code, the “for” loop iterates over the returned worth of the “dict_value.gadgets()” technique and retrieves all of the keys solely by referring to them, and shows them on the console.
Output
The keys of the outlined dictionary are displayed accordingly.
Technique 3: Print Dict Keys Using the “Record Comprehension”
In Python, “Record Comprehension” is a concise/handy, and environment friendly technique for creating lists by iterating over the prevailing listing components. This technique will also be employed to generate the dictionary keys.
Syntax
new_list = [expression for item in iterable if condition]
Instance
The listing comprehension is used within the under code traces to print the dictionary keys:
dict_value = {‘Title’: ‘Anna’, ‘ID’: 1840, ‘Age’: 22}
print(‘Given Dictionary: ‘, dict_value)
[print(keys) for keys in dict_value]
On this code, the “Record Comprehension” method makes use of the “for” loop to iterate over the outlined dictionary and print all of the dictionary keys by focusing on them.
Output
Technique 4: Print Dict Keys Using the “itemgetter()” Operate
The “itemgetter()” perform of the “operator” module retrieves gadgets from an iterable object based mostly on their indices or keys.
Syntax
operator.itemgetter(*gadgets)
Instance
Let’s overview the under instance code:
from operator import itemgetter
dict_value = {‘Title’: ‘Anna’, ‘ID’: 1840, ‘Age’: 22}
print(‘Given Dictionary: ‘, dict_value)
output = listing(map(itemgetter(0), dict_value.gadgets()))
print(‘Dictionary Keys: ‘, output)
Within the above code:
- This “itemgetter” perform is imported from the operator module.
- The dictionary is outlined and displayed.
- The “itemgetter()” perform is used to extract the important thing, as “0” refers back to the index of the “key” within the tuple (key, worth).
- The “map()” perform invokes the “itemgetter()” perform to each tuple worth returned by the “dict_value.gadgets()” technique.
- The output of the “map()” perform is then reworked into a listing using the “listing()” perform.
Output
The above consequence exhibits a listing that comprises the dictionary “keys” solely.
Conclusion
The “dict.keys()” technique, the “dictionary.gadgets()” technique, “Record Comprehension”, or the “itemgetter()” perform are used to print the dictionary “keys” in Python. All of those strategies return the dictionary keys individually, within the type of a key-value pair, or by iterating over the given dictionary. This write-up delivered an in-depth information on how you can print dictionary keys in Python utilizing a number of examples.