| by Arround The Web | No comments

Python Print Dict Keys

Dictionaries” are highly efficient Python data structures that store/keep data in key-value pair format. The “key” in the Python dictionary is a type of immutable data that enables storing and accessing the “values”. But sometimes, we need to print out a dictionary’s keys, either for debugging purposes or to manipulate them.

The purpose of this write-up is to demonstrate the approaches to print the dictionary keys in Python using different methods.

How to Print Dict Keys in Python?

The following methods are utilized to print the keys of a particular Python dictionary:

Method 1: Print Dict Keys Utilizing the “dict.keys()” Method

In Python, the “dict.keys()” method retrieves a view object containing the keys of a dictionary. A dictionary “key” can be accessed and modified utilizing this method.

Syntax

dictionary.keys()

Example

This code example is used to print the dictionary “keys”:

dict_value = {'Name': 'Anna', 'ID': 1840, 'Age': 22}
print('Given Dictionary: ', dict_value)
print(dict_value.keys())

In the above code, the “dict.keys()” method 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 successfully.

Method 2: Print Dict Keys Utilizing the “dictionary.items()” Method

The “dictionary.items()” method retrieves a view object containing the “key-value” pairs of a dictionary. This method accesses both the keys and values of a dictionary together as tuples. The “for” loop can be utilized together with the “dict.items()” method to print only the keys in the dictionary:

Syntax

dictionary.items()

Example

The below code snippet is utilized to print the dictionary “keys”:

dict_value = {'Name': 'Anna', 'ID': 1840, 'Age': 22}
print('Given Dictionary: ', dict_value)
for keys, value in dict_value.items():
    print(keys)

According to the above code, the “for” loop iterates over the returned value of the “dict_value.items()” method and retrieves all the keys only by referring to them, and displays them on the console.

Output

The keys of the defined dictionary are displayed accordingly.

Method 3: Print Dict Keys Utilizing the “List Comprehension”

In Python, “List Comprehension” is a concise/convenient, and efficient method for creating lists by iterating over the existing list elements. This method can also be employed to generate the dictionary keys.

Syntax

new_list = [expression for item in iterable if condition]

Example

The list comprehension is used in the below code lines to print the dictionary keys:

dict_value = {'Name': 'Anna', 'ID': 1840, 'Age': 22}
print('Given Dictionary: ', dict_value)
[print(keys) for keys in dict_value]

In this code, the “List Comprehension” approach utilizes the “for” loop to iterate over the defined dictionary and print all the dictionary keys by targeting them.

Output

Method 4: Print Dict Keys Utilizing the “itemgetter()” Function

The “itemgetter()” function of the “operator” module retrieves items from an iterable object based on their indices or keys.

Syntax

operator.itemgetter(*items)

Example

Let’s overview the below example code:

from operator import itemgetter
dict_value = {'Name': 'Anna', 'ID': 1840, 'Age': 22}
print('Given Dictionary: ', dict_value)
output = list(map(itemgetter(0), dict_value.items()))
print('Dictionary Keys: ', output)

In the above code:

  • This “itemgetter” function is imported from the operator module.
  • The dictionary is defined and displayed.
  • The “itemgetter()” function is used to extract the key, as “0” refers to the index of the “key” in the tuple (key, value).
  • The “map()” function invokes the “itemgetter()” function to every tuple value returned by the “dict_value.items()” method.
  • The output of the “map()” function is then transformed into a list utilizing the “list()” function.

Output

The above result shows a list that contains the dictionary “keys” only.

Conclusion

The “dict.keys()” method, the “dictionary.items()” method, “List Comprehension”, or the “itemgetter()” function are used to print the dictionary “keys” in Python. All of these methods return the dictionary keys separately, in the form of a key-value pair, or by iterating over the given dictionary. This write-up delivered an in-depth guide on how to print dictionary keys in Python using several examples.

Share Button

Source: linuxhint.com

Leave a Reply