Iterate Over Dictionaries in Python & Unpack the Dictionaries

Python is a versatile programming language that offers a wide range of features and functionalities. One of its most useful features is the ability to work with dictionaries, a built-in data structure that allows you to store key-value pairs. You can iterate over dictionaries in Python & unpack the dictionaries using a variety of methods.

In this blog, we will write a Python program to iterate over dictionaries and unpack the dictionaries. We will also demonstrate how to use the *dict method to unpack all the keys in a dictionary and use them in a formatted string. With this tutorial, you will have a solid understanding of how to work with dictionaries in Python and how to leverage their power to make your code more efficient and effective.

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)

Code Explanation

  • Creating the dictionary

We start by creating a dictionary called countryAndCode that contains country names as keys and their corresponding codes as values. The dictionary represents a mapping between countries and their respective codes.

  • Unpacking the dictionary keys

Next, we use the unpacking operator * in conjunction with the dictionary name to iterate over the keys of the dictionary. In our code, we unpack the countryAndCode dictionary and assign the keys to the variable keys.

  • Formatting the values

Next, we format the values of the dictionary using the format method. We provide the unpacked keys as arguments to the format method using the * operator. It expands the dictionary into individual key-value pairs.

  • Printing the results

Finally, we print the keys and values variables, which contain the unpacked keys and the formatted values, respectively. This allows us to see the result of unpacking the dictionary and accessing its contents.

Output:

The output of this code is:

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

In this program, we compiled a code that creates a dictionary called countryAndCode which contains four key-value pairs. Then, our code uses the * operator to unpack the dictionary keys into a list called keys. Now we create a string called values using the format() method for calling the dictionary’s values and printing the keys list and the string of the value. The values string contains the values of the countryAndCode dictionary, separated by hyphens, in the same order as the keys list.

Another method to do the Iterate Over Dictionaries in Python

Apart from using the unpacking technique, you can use the following methods: 

  • Using the keys() method which returns a view object that contains the keys of the dictionary. You can iterate over this view object using a for loop. However, this method only provides access to the keys, not the values. 
  • Using the items() method which returns a view object that contains the key-value pairs of the dictionary. This method allows you to access both the keys and values simultaneously.
  • Using a for loop with dictionary indexing to iterate over the dictionary using a for loop and access the keys and values using dictionary indexing. This method provides more control over accessing specific elements in the dictionary.

The unpacking technique allows you to access the keys and values of a dictionary in a concise and readable manner. It provides a clear and intuitive representation of the dictionary’s contents. It also gives you the flexibility to choose whether you want to access only the keys or both the keys and values.

In this blog, we understood how to iterate over dictionaries in Python using the unpacking technique. By unpacking a dictionary, we can easily access its keys and values separately, allowing us to perform operations on specific elements within the dictionary. The process involves using the * operator to unpack the dictionary keys and the ** operator to unpack the key-value pairs. 

The ability to iterate over dictionaries and unpack their contents is a powerful feature in Python, enabling us to perform various tasks such as formatting, filtering, or manipulating the data. It allows us to work with the individual elements of the dictionary in a straightforward and efficient manner.

By understanding the concepts presented in this code, you can leverage the unpacking technique to access and manipulate dictionaries in your Python programs. 

Overall, the code shows the versatility and convenience of Python’s dictionary unpacking feature, providing you with a valuable tool for handling and processing dictionary data.

FAQ – Based on the above Iterate Over Dictionaries and Unpack

How does the * operator work in unpacking a dictionary?

The * operator before a dictionary name expands the dictionary into individual elements, which in the case of unpacking, are the keys. It separates the keys from the dictionary and assigns them to a variable.

What does the ** operator do in unpacking a dictionary?

The ** operator before a dictionary name separates the key-value pairs of the dictionary. It expands the dictionary into individual key-value pairs, allowing us to access both the keys and values.

What happens if the dictionary contains duplicate keys?

If a dictionary contains duplicate keys, only the last occurrence will be retained. When unpacking the dictionary, the duplicated key will overwrite the previous occurrence.

Can I modify the dictionary while unpacking?

Yes, we can modify the dictionary while unpacking. Any changes made to the dictionary will be reflected when accessing the unpacked keys or values.

Are there any limitations to dictionary unpacking?

One limitation is that dictionary unpacking does not guarantee a specific order of the keys. The order of the unpacked keys may vary, and it’s important to keep this in mind when using the unpacking technique.

About The Author

Leave a Reply