Print keys and values
Write a Python program to print the keys and values of a dictionary.
{"name": "virat", "sports": "cricket"}name Virat sports Cricket
Approach
- Loop over
items()to get both at once.
Solution
player = {"name": "virat", "sports": "cricket"}for key, value in player.items(): print(key, value.capitalize())name Virat sports Cricket
Line by line
items() yields (key, value) pairs that unpack into two variables.
Learn the concept
New to this topic? Read the lesson, then come back.