 # Python – Find dictionary matching value in list

## 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 ``=` `None` `for` `sub ``in` `test_list:` `    ``if` `sub[``'is'``] ``=``=` `7``:` `        ``res ``=` `sub` `        ``break` ` ` `# printing result ` `print``(``"The filtered dictionary value is : "` `+` `str``(res))`

Output :

```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))`

Output :

```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```

Last Updated on November 13, 2021 by admin

## Python – Program to convert a tuple to a stringPython – Program to convert a tuple to a string

Python | Program to convert a tuple to a string Given a tuple of characters, Write a python program to convert the tuple into a string. Examples: Input : ('a', 'b', 'c', 'd', 'e') Output : abcde Input : ('g',

## Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & ilocSelect Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc

Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc Indexing in Pandas means selecting rows and columns of data from a Dataframe. It can be selecting all the rows and the particular

## How to create a list of object in Python classHow to create a list of object in Python class

How to create a list of object in Python class We can create list of object in Python by appending class instances to list. By this, every index in the list can point to instance attributes and methods of the class and can

## Python math sqrt() functionPython math sqrt() function

Python math sqrt() function sqrt() function is an inbuilt function in Python programming language that returns the square root of any number. Syntax: math.sqrt(x) Parameter: x is any number such that x>=0 Returns: It returns the square root of the

## Pass by reference vs value in PythonPass by reference vs value in Python

Pass by reference vs value in Python Developers jumping into Python programming from other languages like C++ and Java are often confused by the process of passing arguments in Python. The object-centric data model and its treatment of assignment is

## Python program to read file word by wordPython program to read file word by word

Python program to read file word by word Given a text file and the task is to read the information from file word by word in Python. Examples: Input: I am R2J! Output: I am R2J! Input: Geeks 4 Geeks

## Python – Get first element of each sublistPython – Get first element of each sublist

Python | Get first element of each sublist Given a list of lists, write a Python program to extract first element of each sublist in the given list of lists. Examples: Input : [[1, 2], [3, 4, 5], [6, 7,

## Remove multiple elements from a Python listRemove multiple elements from a Python list

Given a list of numbers, write a Python program to remove multiple elements from a list based on the given condition. Example: Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] Input: [11, 5, 17,