
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Filter dictionaries with ordered values
When it is required to filter dictionaries with ordered values, the ‘sorted’ method along with the list comprehension is used.
Example
Below is a demonstration of the same
my_list = [{'python': 2, 'is': 8, 'fun': 10}, {'python': 1, 'for': 10, 'coding': 9}, {'cool': 3, 'python': 4}] print("The list is :") print(my_list) my_result = [index for index in my_list if sorted( list(index.values())) == list(index.values())] print("The resultant dictionary is :") print(my_result)
Output
The list is : [{'python': 2, 'fun': 10, 'is': 8}, {'python': 1, 'coding': 9, 'for': 10}, {'python': 4, 'cool': 3}] The resultant dictionary is : [{'python': 1, 'coding': 9, 'for': 10}]
Explanation
A list of dictionary elements is defined and is displayed on the console.
The list comprehension is used to iterate through the elements of the list, and the ‘sorted’ method is used to sort the list by accessing the dictionary values and checking if it is equal to the consecutive element’s value.
This is assigned to a variable.
This variable is displayed as output on the console.
- Related Articles
- What are Ordered dictionaries in Python?
- Python - Filter dictionaries by values in Kth Key in list
- How to create Ordered dictionaries in Python?
- Python – Remove Dictionaries with Matching Values
- Python – Extract dictionaries with values sum greater than K
- Python – Dictionaries with Unique Value Lists
- Python - Filter even values from a list
- Ways to sort list of dictionaries by values in Python
- Ways to sort list of dictionaries using values in python
- Python – Merge Dictionaries List with duplicate Keys
- Python - Filter Rows Based on Column Values with query function in Pandas?
- Python - Filter the negative values from given dictionary
- How to perform Calculations with Dictionaries in Python?
- Python – Filter Tuples with Integers
- Ways to sort list of dictionaries by values in Python Using itemgetter

Advertisements