Convert a Set to Dictionary in Python

Dictionaries in Python store the data in form of key-value pairs where the keys should be unique and immutable. Whereas Sets in Python are used to store a list of unique values inside a single variable. Often there is a need to convert the Set into a Dictionary for a variety of reasons.

For example, consider an e-commerce website that stores various sets of items. We can convert these items into a key-value pair format where keys represent items and values represent their no of units, cost price, etc. Thus in this tutorial, we will learn how to convert a set into a dictionary in Python.

Some of the advantages of using Dictionary overs Sets are:

  1. It improves the readability of the code
  2. It helps in increasing the speed as we only have to look up the keys to getting their corresponding values instead of going over the entire set and searching for the keys and values
  3. It helps in faster analysis of data as the values provide extra information about their corresponding keys

Implementing List comprehension to convert Set to Dictionary in Python

A list comprehension contains a for loop that iterates over each element and executes an expression for that element.

In the below-mentioned example, we define a product_Set that stores a set of product names. We will convert this set into a dictionary by converting it into a key-value pair format. Here the key represents the product name and value is its associated cost. So we create a list comprehension which will use a for loop to iterate over each product in the product set. For each product, we will then associate it with its corresponding cost.

product_set = {'Toys', 'Clothes', 'Books', 'Kitchenware'}
print(f"The products set is:{product_set}")

avg_cost=[200,300,500,250]
for cost in avg_cost:
    dictionary = {product:cost  for product in product_set}

print(dictionary)
print(type(dictionary))

Output:

The products set is:{'Toys', 'Kitchenware', 'Clothes', 'Books'}
{'Toys': 250, 'Kitchenware': 250, 'Clothes': 250, 'Books': 250}
<class 'dict'>

Implementing fromkeys() to convert Set to Dictionary

The fromkeys() function in Python is used to generate a dictionary from the specified keys and values as the arguments. The syntax of the fromkeys() function is: fromkeys (key,value). The keys can represent any data structure such as a list or set etc which contains keys. Therefore in the below-mentioned code, the keys take the entire product_set as an input. The values represent the cost associated with each product name from the avg_cost set. Thus we successfully generate a dictionary from the product set.

product_set = {'Toys', 'Clothes', 'Books', 'Kitchenware'}
print(f"The products set is:{product_set}")

avg_cost=[200,300,500,250]
for cost in avg_cost:
    dictionary = dict.fromkeys(product_set,cost)

print(dictionary)
print(type(dictionary))

Output:

The products set is:{'Books', 'Clothes', 'Kitchenware', 'Toys'}
{'Books': 250, 'Clothes': 250, 'Kitchenware': 250, 'Toys': 250}
<class 'dict'>

Implementing zip and dict to convert Set to Dictionary

The zip() function in python takes a data structure or iterable such as a list or set as an argument and returns a zip object. The zip object maps the first item of each iterable together and continues this mapping till all the items have been mapped together. The dict() function is used to convert an iterable into a Python Dictionary. In the below-mentioned example, we initially give two sets (product_set and avg_cost) as an input to the zip() function. This function will map each product name to its corresponding cost and return a zip object. Then we convert this zip object to a Dictionary using the dict() function.

product_set = {'Toys', 'Clothes', 'Books', 'Kitchenware'}
print(f"The products set is:{product_set}")

avg_cost=[200,300,500,250]

dictionary = dict(zip(product_set,avg_cost))

print(dictionary)
print(type(dictionary))

Output:

The products set is:{'Clothes', 'Books', 'Kitchenware', 'Toys'}
{'Clothes': 200, 'Books': 300, 'Kitchenware': 500, 'Toys': 250}
<class 'dict'>

Thus we have reached the end of this tutorial on how to convert Set to Dictionary in Python. To read more about the sets in Python refer to the following mentioned links:

Sets in Python: Operations on Set Objects in Python with Examples

Leave a Reply

Your email address will not be published.