Iterate Over Dictionaries in Python Using Items() Function

(Last Updated On: 28/12/2022)

Iterate over dictionaries in python using for loop through all key, and value pairs using items()

Python Program to Iterate Over Dictionaries Using Items() Function

# Iterate Over Dictionaries in Python Using Items() Function
# Iterate through all key, and value pairs using items()

countryAndCode = {
	'India': 'IND',
	'Afghanistan': 'AFG',
	'Canada': 'CAN',
	'Russia': 'RUS'
}

print('List of given Country and their Code:\n')

# Iterating over values
#  printing all the key and value pairs present in a dictionary using a items() method.
for country, code in countryAndCode.items():
	print(country, ":", code)

Output:

List of given Country and their Code:

India : IND
Afghanistan : AFG
Canada : CAN
Russia : RUS

Leave a Reply

Your email address will not be published. Required fields are marked *