Python Program To Get the Keys of Dictionaries
In this example shows, We will sort the dictinary by keys and values ,the result will be display the type of dictionary.
Using a loop
Example.py
a = {'Roll_No' : 101, 'Name' : 'Kumar', 'Mobile': 6234786745, 'City': 'salem'} print("Dictionary \'a\' is...") print(a) print("Keys of the Dictionary \'a\'...") for i in a: print(i)
Output
Dictionary 'a' is... {'Roll_No': 101, 'Name': 'Kumar', 'Mobile': 6234786745, 'City': 'salem'} Keys of the Dictionary 'a'... Roll_No Name Mobile City
Using keys() method
Example.py
a = {'Roll_No' : 101, 'Name' : 'Kumar', 'Mobile': 6234786745, 'City': 'salem'} keys = a.keys() print(list(keys))
Output
['Roll_No', 'Name', 'Mobile', 'City']
Using list() method
Example.py
a = {'Roll_No' : 101, 'Name' : 'Kumar', 'Mobile': 6234786745, 'City': 'salem'} keys = list(a) print(keys)
Output
['Roll_No', 'Name', 'Mobile', 'City']