Python Practice › Loop Programs › Program 68
Print keys and values
Write a Python program to print the keys and values of a dictionary.
Input
{"name": "virat", "sports": "cricket"}Output
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())Output
name Virat sports Cricket
Line by line
Line 2
items() yields (key, value) pairs that unpack into two variables.
Learn the concept
New to this topic? Read the lesson, then come back.