Iterate Over Dictionaries in Python & Unpack the Dictionaries

(Last Updated On: 28/12/2022)

Python Program to Iterate Over Dictionaries and Unpack the Dictionaries

# Iterate Over Dictionaries in Python & Unpack the Dictionaries
# Access key using unpacking of dict

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

# we are using * to unpack the dict.
keys = [*countryAndCode]
# The *dict method which helps us to unpack all the keys in the dictionary.
values = '{India}-{Afghanistan}-{Canada}-{Russia}'.format(*countryAndCode, **countryAndCode)
print(keys)
print(values)

Output:

['India', 'Afghanistan', 'Canada', 'Russia']
IND-AFG-CAN-RUS

Leave a Reply

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