Python | Find dictionary matching value in list
The problem of getting only the suitable dictionary that has a particular value of the corresponding key is quite common when one starts working with dictionary. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop
This is the brute force method by which this task can be performed. For this, we just use naive check and compare and return the result once we find the suitable match and break for rest of dictionaries.
# Python3 code to demonstrate working of # Find dictionary matching value in list # Using loop # Initialize list test_list = [{'gfg' : 2, 'is' : 4, 'best' : 6}, {'it' : 5, 'is' : 7, 'best' : 8}, {'CS' : 10}] # Printing original list print("The original list is : " + str(test_list)) # Using loop # Find dictionary matching value in list res = Nonefor sub in test_list: if sub['is'] == 7: res = sub break # printing result print("The filtered dictionary value is : " + str(res)) |
The original list is : [{'is': 4, 'gfg': 2, 'best': 6}, {'is': 7, 'best': 8, 'it': 5}, {'CS': 10}]
The filtered dictionary value is : {'is': 7, 'best': 8, 'it': 5}
Method #2 : Using next() + dictionary comprehension
The combination of these methods can also be used to perform this task. This difference is that it’s a one liner and more efficient as next function uses iterator as internal implementation which are quicker than generic methods.
# Python3 code to demonstrate working of # Find dictionary matching value in list # Using next() + dictionary comprehension # Initialize list test_list = [{'gfg' : 2, 'is' : 4, 'best' : 6}, {'it' : 5, 'is' : 7, 'best' : 8}, {'CS' : 10}] # Printing original list print("The original list is : " + str(test_list)) # Using next() + dictionary comprehension # Find dictionary matching value in list res = next((sub for sub in test_list if sub['is'] == 7), None) # printing result print("The filtered dictionary value is : " + str(res)) |
The original list is : [{'is': 4, 'gfg': 2, 'best': 6}, {'is': 7, 'best': 8, 'it': 5}, {'CS': 10}]
The filtered dictionary value is : {'is': 7, 'best': 8, 'it': 5}
Recommended Posts:
- Python | Indices list of matching element from other list
- Python | Group by matching second tuple value in list of tuples
- Python | Clearing list as dictionary value
- Python | Updating value list in dictionary
- Python | Convert a list to dictionary
- Python | Get dictionary keys as a list
- Python | List value merge in dictionary
- Python | Convert dictionary to list of tuples
- Python | Dictionary creation using list contents
- Python | Converting list string to dictionary
- Python | Initialize a dictionary with only keys from a list
- Python | List of tuples to dictionary conversion
- Python | Grouping list values into dictionary
- Python | Create a dictionary using list with none values
- Python | Removing dictionary from list of dictionaries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



