Iterate Over Dictionaries in Python Using Both Key() & Value() Function

(Last Updated On: 28/12/2022)

Iterate over dictionaries in python using for loop through access both key() and value() without using items()

Python Program to Iterate Over Dictionaries Using Both Key() & Value() Function

# Iterate Over Dictionaries in Python Using Both Key() & Value() Function
# Access both key and value without using items()

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

for i in countryAndCode:
# we are using a key as an index to print values from the directory
	print(i, '->', countryAndCode[i])

Output:

India -> IND
Afghanistan -> AFG
Canada -> CAN
Russia -> RUS

Leave a Reply

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