Python program to extract dictionary items for custom values


Custom values are specific values that are defined and selected under certain criteria. In a large and complex dataset, specification is very important for constructing an optimized program and therefore extraction of relevant data becomes very important. We can pass a reference list according to which the values can be extracted.

This reference list has its own implications as it stores concise information. In this article we will be discussing a similar concept of extracting dictionary items (both keys and values) for a custom data passed through a list.

Understanding the Problem

We will create a dictionary consisting of certain data. Each key will be associated with different values and then we will pass a reference list that stores numerous values. Those dictionary items where the dictionary value is equal to the reference value will be extracted i.e., all values from the dictionary that match the list value should be isolated along with their corresponding “key”.

Input Output Scenarios

Let us consider a dictionary with the following values −

Input:
dict1 = {"Grapes":12, "Mangoes":18, "Apples":10, "Strawberries":4, "Coconut":24}
lis1 = [13, 18, 4, 22]

As we can see, the values “18” and “4” from the reference list match the values of dictionary keys “Mangoes” and “Strawberries”. Therefore, the following output should be returned −

Output: {'Mangoes': 18, 'Strawberries': 4}

Now that we have understood the problem statement, let’s discuss a few solutions.

Using Loops (iterations)

After creating the dictionary, we will use a for loop to iterate over the keys and values. We will create an empty dictionary which will store only the extracted dictionary items.

We will also iterate over the reference list and then match each and every list value with different dictionary values. We will establish an “if” condition to check for these matching values and then store them into an empty dictionary.

Example

Following is an example to extract dictionary items for custom values using loops −

dict1 = {"Grapes":12, "Mangoes":18, "Apples":10, "Strawberries":4, "Coconut":24}
lis1 = [13, 18, 4, 22]
dict2 = {}

print(f"The original dictionary is: {dict1}")
for keys, values in dict1.items():

   for x in lis1:
      if values == x:
         dict2[keys] = values

print(f"The new dictionary consisting of custom values: {dict2}")

Output

The original dictionary is: {'Grapes': 12, 'Mangoes': 18, 'Apples': 10, 'Strawberries': 4, 'Coconut': 24}
The new dictionary consisting of custom values: {'Mangoes': 18, 'Strawberries': 4}

Using Dictionary Comprehension

Another efficient and clean way of executing this program involves the use of dictionary comprehension. With the help of this technique, we can produce a single line code that covers the entire iteration concept. This approach allows us to prepare a concise and highly readable program to extract dictionary items.

Example

Following is an example −

dict1 = {"Grapes":12, "Mangoes":18, "Apples":10, "Strawberries":4, "Coconut":24}
lis1 = [13, 18, 4, 22]

print(f"The original dictionary is: {dict1}")
dict2 = {keys: values for keys, values in dict1.items() if values in lis1}

print(f"The new dictionary consisting of custom values: {dict2}")

Output

The original dictionary is: {'Grapes': 12, 'Mangoes': 18, 'Apples': 10, 'Strawberries': 4, 'Coconut': 24}
The new dictionary consisting of custom values: {'Mangoes': 18, 'Strawberries': 4}

Using Lambda Function + filter() function

In this approach, we will use the filter() function to select the dictionary values that are equal to the reference list values. We will also pass a lambda function as the first parameter for this filter() function.

The lambda function will act as an anonymous filtering function that determines whether a dictionary item should be present in the filtered dictionary. We will pass the original dictionary as the second parameter for the filter() function. “items[1]” refers to the values associated with each key-value pair.

Example

Following is an example −

dict1 = {"Grapes":12, "Mangoes":18, "Apples":10, "Strawberries":4, "Coconut":24}
lis1 = [13, 18, 4, 22]

print(f"The original dictionary is: {dict1}")
dict2 = dict(filter(lambda items: items[1] in lis1, dict1.items()))

print(f"The new dictionary consisting of custom values: {dict2}")

Output

The original dictionary is: {'Grapes': 12, 'Mangoes': 18, 'Apples': 10, 'Strawberries': 4, 'Coconut': 24}
The new dictionary consisting of custom values: {'Mangoes': 18, 'Strawberries': 4}

Other Solutions and Valuable Insights

There are several other solutions that include the use of “map()” function along with lambda and filter functions. We can also convert the dictionary and list values into “sets” and then retrieve the common values. The retrieval of these custom values plays an important role in data visualization and dataset management. We can also retrieve other data types by creating a generalized program that covers all type of values.

Conclusion

During the course of this article, we discussed the various solutions to extract dictionary items for custom values. Firstly, we used a simple iteration approach to traverse through both the dictionary and reference list values. In our 2nd approach, we used the concept of dictionary comprehension and for the final solution we used the filter() and lambda functions. A last we discussed a few alternative solutions and some valuable insights related to custom value extraction.

Updated on: 12-Jul-2023

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements