Computer >> Computer tutorials >  >> Programming >> Python

How to print all the keys of a dictionary in Python?


Dictionary object possesses keys() method which does this job for us.

>>> D1 = {1:'a', 2:'b',3:'c'}
>>> D1.keys()
dict_keys([1, 2, 3])
>>> list(D1.keys())
   [1, 2, 3]

iterable list object can be traversed using for loop

>>> L1 = list(D1.keys())
>>> for i in L1:
   print (i)
1
2
3