Sunday 27 December 2020

Python: Looping through all values in a dictionary

‘values’ method is used to loop through all the values in a dictionary.

>>> country_capitals = {"India" : "New Delhi", "Bangladesh" : "Dhaka", "France" : "Paris"}
>>> 
>>> for capital in country_capitals.values():
...   print(f"{capital}")
... 
New Delhi
Dhaka
Paris

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment