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

PythonServer Side ProgrammingProgramming

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
raja
Published on 28-Dec-2017 06:00:17
Advertisements