This post will discuss how to search a value for a given key in a list of dictionaries in Python.

1. Using filter() function

The filter() is a built-in function that returns an iterator of items from an iterable that satisfy a given condition. We can use this function to search for a value in a list of dictionaries by passing a lambda expression that checks if the value matches one or more of the dictionary’s values as the first argument and the list of dictionaries as the second argument. For example:

Download  Run Code

2. Using Generator Expression

Another solution is to use a generator expression for searching a value for a given key in a list of dictionaries. This would translate to a simple code below:

Download  Run Code

3. Using for loop

We can use a for loop to search for a value in a list of dictionaries by looping over the list and checking if the value matches one or more of the dictionary’s values. For example, we can search for the value ‘Python’ in the list of dictionaries using a for loop like this:

Download  Run Code

That’s all about searching for a value in a list of dictionaries in Python.