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))
|
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
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))
|
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
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))
|
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
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))
|
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.