Open In App

Filter List Of Dictionaries in Python

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Filtering a list of dictionaries is a fundamental programming task that involves selecting specific elements from a collection of dictionaries based on defined criteria. This process is commonly used for data manipulation and extraction, allowing developers to efficiently work with structured data by narrowing down the dataset to include only relevant information.

Filter List Of Dictionaries in Python

Below, are the examples of Filter List Of Dictionaries in Python.

Filter List Of Dictionaries Using List Comprehension

In this example, the below code filters a list of dictionaries (`original_list`) to create a new list (`filtered_list`) containing only dictionaries where the ‘age’ key is greater than 25 and then prints the result.

Python3




original_list = [{'name': 'Alice', 'age': 25},
                 {'name': 'Bob', 'age': 30},
                 {'name': 'Charlie', 'age': 22}]
  
filtered_list = [d for d in original_list if d['age'] > 25]
  
print(filtered_list)


Output

[{'name': 'Bob', 'age': 30}]


Filter List Of Dictionaries Using Filter() and Lambda Function

In this example, below code filters a list of dictionaries (`original_list`) using the `filter()` function and a lambda expression to include only dictionaries where the ‘age’ key is greater than 25. The result is stored in `filtered_list` .

Python3




original_list = [{'name': 'Alice', 'age': 25},
                 {'name': 'Bob', 'age': 30},
                 {'name': 'Charlie', 'age': 22}]
  
filtered_list = list(filter(lambda d: d['age'] > 25, original_list))
  
print(filtered_list)


Output

[{'name': 'Bob', 'age': 30}]


Filter List Of Dictionaries Using Pandas Library

In this example , below code converts a list of dictionaries (`original_list`) into a Pandas DataFrame (`df`) and then filters the DataFrame to include only rows where the ‘age’ column is greater than 25. The filtered result is converted back to a list of dictionaries (`filtered_df`) .

Python3




import pandas as pd
  
original_list = [{'name': 'Alice', 'age': 25},
                 {'name': 'Bob', 'age': 30},
                 {'name': 'Charlie', 'age': 22}]
  
df = pd.DataFrame(original_list)
filtered_df = df[df['age'] > 25].to_dict(orient='records')
  
print(filtered_df)


Output

[{'name': 'Bob', 'age': 30}]


Similar Reads

Python - Convert Dictionaries List to Order Key Nested dictionaries
Given list of dictionaries, convert to ordered key dictionary with each key contained dictionary as its nested value. Input : test_list = [{"Gfg" : 3, 4 : 9}, {"is": 8, "Good" : 2}] Output : {0: {'Gfg': 3, 4: 9}, 1: {'is': 8, 'Good': 2}} Explanation : List converted to dictionary with index keys. Input : test_list = [{"is": 8, "Good" : 2}] Output :
6 min read
Python Program to extract Dictionaries with given Key from a list of dictionaries
Given a list of dictionaries, the task is to write a python program that extracts only those dictionaries that contain a specific given key value. Input : test_list = [{'gfg' : 2, 'is' : 8, 'good' : 3}, {'gfg' : 1, 'for' : 10, 'geeks' : 9}, {'love' : 3}], key= "gfg"Output : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg' : 1, 'for' : 10, 'geeks' : 9}] Expl
6 min read
Convert Dictionary of Dictionaries to Python List of Dictionaries
Dictionaries are powerful data structures in Python, allowing the storage of key-value pairs. Sometimes, we encounter scenarios where we have a dictionary of dictionaries, and we need to convert it into a list of dictionaries for easier manipulation or processing. In this article, we'll explore five different methods to achieve this conversion, eac
3 min read
Convert a List of Dictionaries into a Set of Dictionaries
Python's versatility allows developers to manipulate data in various ways. When working with a list of dictionaries, there might be scenarios where you want to convert it into a set of dictionaries to eliminate duplicates or for other reasons. In this article, we'll explore three different methods to achieve this goal with code examples. Convert A
3 min read
Filter List of Python Dictionaries by Key in Python
Filtering a list of dictionaries in Python based on a specific key is a common task in data manipulation and analysis. Whether you're working with datasets or dictionaries, the ability to extract relevant information efficiently is crucial. In this article, we will explore some simple and commonly used methods to filter a list of dictionaries in Py
3 min read
Python - Filter dictionaries by values in Kth Key in list
Given a list of dictionaries, the task is to write a Python program to filter dictionaries on basis of elements of Kth key in the list. Examples: Input : test_list = [{"Gfg" : 3, "is" : 5, "best" : 10}, {"Gfg" : 5, "is" : 1, "best" : 1}, {"Gfg" : 8, "is" : 3, "best" : 9}, {"Gfg" : 9, "is" : 9, "best" : 8}, {"Gfg" : 4, "is" : 10, "best" : 7}], K = "
9 min read
Filter List of Dictionaries Based on Key Values in Python
Filtering a list of dictionaries based on key values is a common task in Python, especially when working with data manipulation and analysis. In this article, we will explore five different methods to achieve this goal, each offering its own advantages and use cases. Whether you're a beginner or an experienced Python developer, these methods will p
3 min read
Python Filter List of Dictionaries Based on Key Value
Python, a versatile and powerful programming language, offers multiple ways to manipulate and process data. When working with a list of dictionaries, you may often need to filter the data based on specific key-value pairs. In this article, we will explore three different methods to achieve this task: using list comprehension, the filter function, a
3 min read
Python - Filter dictionaries with ordered values
Given the dictionary list, the task is to write a python program to filter dictionaries with values in increasing order i.e sorted. Examples: Input : test_list = [{'gfg' : 2, 'is' : 8, 'good' : 10}, {'gfg' : 1, 'for' : 10, 'geeks' : 9}, {'love' : 3, 'gfg' : 4}] Output : [{'gfg': 2, 'is': 8, 'good': 10}, {'love': 3, 'gfg': 4}] Explanation : 2, 8, 10
4 min read
Python - Convert List of Dictionaries to List of Lists
Sometimes, while working with Python data, we can have a problem in which we need to convert the list of dictionaries into a list of lists, this can be simplified by appending the keys just once if they are repetitive as mostly in records, this saves memory space. This type of problem can have applications in the web development domain. Let's discu
7 min read
Practice Tags :