All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Python Filter List Of Dictionaries

Last Updated : Mar 11, 2024

Python Filter List Of Dictionaries

In this article we will show you the solution of python filter list of dictionaries, a list of dictionaries can be effectively filtered using the potent built-in function filter() provided by the Python programming language.

With the help of this function, you may quickly extract and modify data subsets to suit your needs.

A filtering criterion and the list to be filtered are the two inputs for the filter() function.

Usually, a lambda function or regular function is used to define the condition, evaluating each item in the list.

With dictionaries, you may use their keys to access particular values.

You can write clear, expressive code to only extract the desired dictionaries that satisfy your filtering requirement by using the filter() function on a list of dictionaries.

With the help of filter(), you may speed up your data processing and analysis regardless of whether you want to locate dictionaries with particular key-value combinations, filter based on particular values, or use any other custom criteria.

Now move to the concept of python filter list of dictionaries.

Step By Step Guide On Python Filter List Of Dictionaries :-

# Sample list of dictionaries
data = [
    {"name": "Aman", "age": 25, "city": "New Delhi"},
    {"name": "Baban", "age": 30, "city": "Latur"},
    {"name": "Chaman", "age": 35, "city": "Panvel"},
    {"name": "Damu", "age": 40, "city": "New Delhi"},
    {"name": "Ramu", "age": 45, "city": "Latur"}
# Filter function to find dictionaries with age greater than 30
def filter_by_age(dictionary):
    return dictionary["age"] > 30
# Filter the list of dictionaries
filtered_data = list(filter(filter_by_age, data))
# Print the filtered results
for item in filtered_data:
  1. We have a collection of dictionaries called data that include details about certain people.
  2. Each dictionary depicts a certain individual and has keys like "name," "age," and "city."
  3. Our objective is to remove simply the dictionaries for the ones over the age of 30 from this listing of dictionaries.
  4. To do this, we create the filter_by_age() approach, which accepts a dictionary as input.
  5. In this function, the "age" key's used to reap the dictionary's "age" value, that's then as compared to the threshold price of 30.
  6. We return True, indicating that the dictionary should be included within the filtered outcomes, if the age is more than 30.
  7. If now not, we deliver again False, which denotes exclusion from the effects of the filtering.
  8. The clear out() feature is then used, which calls for arguments: a list of dictionaries (facts) and the filtering situation (in this case, the filter_by_age() characteristic).
  9. Each dictionary inside the records list is problem to the situation after it's far implemented through the filter out() technique, which then produces an iterator that only carries the dictionaries that match the condition.
  10. We enclose the filter out() approach within the listing() constructor to transform this iterator right into a list.
  11. The filtered_data variable holds the filtered results.
  12. We then use a for loop to repeatedly iterate over this list, where each item stands in for a filtered dictionary.
  13. Using the print() method inside the loop, we display each filtered dictionary.
  14. When this code is run, the original data list will be filtered according to age criteria, and anyone over the age of 30 will only have their dictionaries printed.
  15. In this situation, since Chaman, Damu, and Ramu satisfy the filtering requirements, the dictionaries for Chaman, Damu, and Ramu will be output.

Conclusion :-

As a result, we have been capable of understand the concept in the back of the Python filter out list of dictionaries.

Additionally, we determined how simple it's miles to sort a listing of dictionaries the usage of the filter() characteristic in Python.

I hope this article on python filter list of dictionaries helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪