

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- What are Ordered dictionaries in Python?
- How to create Ordered dictionaries in Python?
- Python - Filter dictionaries by values in Kth Key in list
- 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 - Filter Rows Based on Column Values with query function in Pandas?
- How to perform Calculations with Dictionaries in Python?
- Python – Merge Dictionaries List with duplicate Keys
- Python - Filter the negative values from given dictionary
- How to sort a list of dictionaries by values of dictionaries in C#?
- Python – Filter Tuples with Integers
Advertisements