Python - Filter dictionaries with ordered values
Last Updated :
04 May, 2023
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 are in increasing order.
Input : test_list = [{'gfg' : 2, 'is' : 8, 'good' : 10}, {'gfg' : 1, 'for' : 10, 'geeks' : 9}, {'love' : 4, 'gfg' : 3}]
Output : [{'gfg': 2, 'is': 8, 'good': 10}]
Explanation : 2, 8, 10 are in increasing order.
Method #1 : Using sorted() + values() + list comprehension
In this, we perform task of sorting using sorted() and extract values using values(), list comprehension is used to perform iteration of all the dictionaries.
Python3
# Python3 code to demonstrate working of
# Filter dictionaries with ordered values
# Using sorted() + values() + list comprehension
# initializing list
test_list = [{'gfg': 2, 'is': 8, 'good': 10},
{'gfg': 1, 'for': 10, 'geeks': 9},
{'love': 3, 'gfg': 4}]
# printing original list
print("The original list is : " + str(test_list))
# sorted to check with ordered values
# values() extracting dictionary values
res = [sub for sub in test_list if sorted(
list(sub.values())) == list(sub.values())]
# printing result
print("The filtered Dictionaries : " + str(res))
OutputThe original list is : [{'gfg': 2, 'is': 8, 'good': 10}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3, 'gfg': 4}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 10}, {'love': 3, 'gfg': 4}]
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + sorted()
In this, we perform task of filtering using filter(), and lambda function is used to perform task of injecting functionality necessary for checking increasing values.
Python3
# Python3 code to demonstrate working of
# Filter dictionaries with ordered values
# Using filter() + lambda + sorted()
# initializing list
test_list = [{'gfg': 2, 'is': 8, 'good': 10},
{'gfg': 1, 'for': 10, 'geeks': 9},
{'love': 3, 'gfg': 4}]
# printing original list
print("The original list is : " + str(test_list))
# sorted to check with ordered values
# values() extracting dictionary values
# filter() and lambda function used to filter
res = list(filter(lambda sub: sorted(list(sub.values()))
== list(sub.values()), test_list))
# printing result
print("The filtered Dictionaries : " + str(res))
OutputThe original list is : [{'gfg': 2, 'is': 8, 'good': 10}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3, 'gfg': 4}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 10}, {'love': 3, 'gfg': 4}]
Time Complexity: O(nlogn)
Auxiliary Space: O(k)
Method #3 : Using values(),extend() and sort() methods
Python3
# Python3 code to demonstrate working of
# Filter dictionaries with ordered values
# initializing list
test_list = [{'gfg': 2, 'is': 8, 'good': 10},
{'gfg': 1, 'for': 10, 'geeks': 9},
{'love': 3, 'gfg': 4}]
# printing original list
print("The original list is : " + str(test_list))
res = []
for i in test_list:
y = []
x = list(i.values())
y.extend(x)
y.sort()
if(x == y):
res.append(i)
# printing result
print("The filtered Dictionaries : " + str(res))
OutputThe original list is : [{'gfg': 2, 'is': 8, 'good': 10}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3, 'gfg': 4}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 10}, {'love': 3, 'gfg': 4}]
Time Complexity: O(nlogn), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #4: Using a loop and a flag variable to check for ordered values
Python3
# Python3 code to demonstrate working of
# Filter dictionaries with ordered values
# Using a loop and a flag variable
# Initializing list
test_list = [{'gfg': 2, 'is': 8, 'good': 10},
{'gfg': 1, 'for': 10, 'geeks': 9},
{'love': 3, 'gfg': 4}]
# Printing original list
print("The original list is : " + str(test_list))
# Looping through each dictionary and
# check for ordered values
res = []
for d in test_list:
values = list(d.values())
ordered = True
for i in range(len(values) - 1):
if values[i] > values[i+1]:
ordered = False
break
if ordered:
res.append(d)
# Printing the result
print("The filtered Dictionaries : " + str(res))
OutputThe original list is : [{'gfg': 2, 'is': 8, 'good': 10}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3, 'gfg': 4}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 10}, {'love': 3, 'gfg': 4}]
Time complexity: O(n*m), where n is the number of dictionaries in the list and m is the maximum number of values in a dictionary.
Auxiliary space: O(m), since we are creating a list of dictionary values for each dictionary in the list.
Similar Reads
Python | Extract filtered Dictionary Values While working with Python dictionaries, there can be cases in which we are just concerned about getting the filtered values list and donât care about keys. This is yet another essential utility and solution to it should be known and discussed. Letâs perform this task through certain methods. Method
4 min read
Python | Filter Tuple Dictionary Keys Sometimes, while working with Python dictionaries, we can have itâs keys in form of tuples. A tuple can have many elements in it and sometimes, it can be essential to get them. If they are a part of a dictionary keys and we desire to get filtered tuple key elements, we need to perform certain functi
4 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
9 min read
Reverse Dictionary Keys Order - Python We are given a dictionary and our task is to reverse the order of its keys. This means if we have a dictionary like {'a': 1, 'b': 2, 'c': 3} then the output will be {'c': 3, 'b': 2, 'a': 1}. This can be done using methods like reversed(), dictionary comprehensions, or OrderedDict. Let's explore thes
4 min read
Python - Filter odd elements from value lists in dictionary Sometimes, while working with Python dictionaries we can have problem in which we need to perform the removal of odd elements from values list of dictionaries. This can have application in many domains including web development. Lets discuss certain ways in which this task can be performed. Method
6 min read
Sorting Python Dictionary With Lists as Values Python offers various methods to sort a dictionary with Lists as Values. This is a common task when dealing with data where you need to order elements based on different criteria. In this article, we will sort a dictionary with lists as values in Python. Sort a Dictionary with Lists as Values in Pyt
3 min read
Python | Filter the negative values from given dictionary Given a dictionary, the task is to filter all the negative values from given dictionary. Let's discuss few methods to do this task. Method #1: Using dict comprehension Follow the below steps to implement: Initializing a dictionary named ini_dict with some key-value pairs.Print the initial dictionary
6 min read
Python - Filter dictionary values in heterogeneous dictionary Sometimes, while working with Python dictionaries, we can have a problem in which we need to filter out certain values based on certain conditions on a particular type, e.g all values smaller than K. This task becomes complex when dictionary values can be heterogeneous. This kind of problem can have
6 min read
Filter List Of Dictionaries in Python 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 b
2 min read
Sort a List of Python Dictionaries by a Value Sorting a list of dictionaries by a specific value is a common task in Python programming. Whether you're dealing with data manipulation, analysis, or simply organizing information, having the ability to sort dictionaries based on a particular key is essential. In this article, we will explore diffe
3 min read