Sunday, 27 December 2020

Python: Looping over a dictionary using keys

Using keys() method

‘keys()’ method return an iterator over all the keys of a dictionary.

 

Example

for key in emps.keys():

    print(f"{key} : {emps[key]}")

>>> emps = {1: {'Hari Krishna', 'Gurram'}, 3: {'Shekkappa', 'Mohan'}, 4: {'Ranganath', 'Thippisetty'}, 5: {'Ganji', 'Sudheer'}}
>>> 
>>> for key in emps.keys():
...   print(f"{key} : {emps[key]}")
... 
1 : {'Gurram', 'Hari Krishna'}
3 : {'Mohan', 'Shekkappa'}
4 : {'Thippisetty', 'Ranganath'}
5 : {'Sudheer', 'Ganji'}

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment