Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Method 1: Using For Loops
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}")
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}
Method 2: 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}")
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}
Method 3: Using Lambda Function + filter()
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}")
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}
Performance Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| For Loops | O(n*m) | O(k) | Beginners |
| Dictionary Comprehension | O(n*m) | O(k) | Readable code |
| Lambda + filter() | O(n*m) | O(k) | Functional programming |
Where n is the number of dictionary items, m is the number of custom values, and k is the number of matching items.
Conclusion
Dictionary comprehension provides the most readable and Pythonic solution for extracting dictionary items based on custom values. Use the lambda + filter() approach for functional programming style, and traditional loops when you need more complex filtering logic.
