Open In App

Python – Filter dictionaries with ordered values

Last Updated : 04 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


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




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


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




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


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




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


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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads