Open in App
Not now

Python – Filter dictionaries with ordered values

• Last Updated : 26 Oct, 2022

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}]
```

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}]
```

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}]
```

My Personal Notes arrow_drop_up
Related Articles