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
test_list = [{ 'gfg' : 2 , 'is' : 8 , 'good' : 10 },
{ 'gfg' : 1 , 'for' : 10 , 'geeks' : 9 },
{ 'love' : 3 , 'gfg' : 4 }]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if sorted (
list (sub.values())) = = list (sub.values())]
print ( "The filtered Dictionaries : " + str (res))
|
Output
The 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
test_list = [{ 'gfg' : 2 , 'is' : 8 , 'good' : 10 },
{ 'gfg' : 1 , 'for' : 10 , 'geeks' : 9 },
{ 'love' : 3 , 'gfg' : 4 }]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda sub: sorted ( list (sub.values()))
= = list (sub.values()), test_list))
print ( "The filtered Dictionaries : " + str (res))
|
Output
The 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
test_list = [{ 'gfg' : 2 , 'is' : 8 , 'good' : 10 },
{ 'gfg' : 1 , 'for' : 10 , 'geeks' : 9 },
{ 'love' : 3 , 'gfg' : 4 }]
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)
print ( "The filtered Dictionaries : " + str (res))
|
Output
The 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
test_list = [{ 'gfg' : 2 , 'is' : 8 , 'good' : 10 },
{ 'gfg' : 1 , 'for' : 10 , 'geeks' : 9 },
{ 'love' : 3 , 'gfg' : 4 }]
print ( "The original list is : " + str (test_list))
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)
print ( "The filtered Dictionaries : " + str (res))
|
Output
The 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 Merge Dictionaries with Same Keys
When working with dictionaries in Python, you should understand that they are mutable, unordered collections of key-value pairs. This flexibility allows us to quickly merge dictionaries, but what happens when two dictionaries have the same key? Python allows us to manage this situation. In this arti
3 min read
Python | Initialize dictionary with None values
Sometimes, while working with dictionaries, we might have a utility in which we need to initialize a dictionary with None values so that they can be altered later. This kind of application can occur in cases of memoization in general or competitive programming. Let's discuss certain ways in which th
4 min read
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 - Dictionary with Index as Value
We are having a list we need to find index of each element and store it in form of dictionary. For example, a = ['a', 'b', 'c', 'd'] we need to find index of each elements in list so that output should be {'a': 0, 'b': 1, 'c': 2, 'd': 3}. Using Dictionary ComprehensionWe can use dictionary comprehen
2 min read
Python - Dictionaries with Unique Value Lists
Given List of dictionaries with list values, extract unique dictionaries. Input : [{'Gfg': [2, 3], 'is' : [7, 8], 'best' : [10]}, {'Gfg': [2, 3], 'is' : [7, 8], 'best' : [10]}] Output : [{'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}] Explanation : Both are similar dictionaries, and hence 1 is removed.
4 min read
Python - Extract dictionaries with values sum greater than K
Given a dictionaries list, extract all the dictionaries whose keys summation exceeds K. Input : test_list = [{"Gfg" : 4, "is" : 8, "best" : 9}, {"Gfg" : 3, "is": 7, "best" : 5}], K = 15 Output : [{'Gfg': 4, 'is': 8, 'best': 9}] Explanation : 4 + 9 + 8 = 21. Greater than K, hence returned. Input : te
8 min read
Python Iterate Dictionary Key, Value
In Python, a Dictionary is a data structure that stores the data in the form of key-value pairs. It is a mutable (which means once created we modify or update its value later on) and unordered data structure in Python. There is a thing to keep in mind while creating a dictionary every key in the dic
3 min read
Inverse Dictionary Values List - Python
We are given a dictionary and the task is to create a new dictionary where each element of the value lists becomes a key and the original keys are grouped as lists of values for these new keys.For example: dict = {1: [2, 3], 2: [3], 3: [1]} then output will be {2: [1], 3: [1, 2], 1: [3]} Using defau
2 min read
Python - Iterate over Tuples in Dictionary
In this article, we will discuss how to Iterate over Tuples in Dictionary in Python. Method 1: Using index We can get the particular tuples by using an index: Syntax: dictionary_name[index] To iterate the entire tuple values in a particular index for i in range(0, len(dictionary_name[index])): print
2 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