- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 by values in Kth Key in list
When it is required to filter dictionaries by values in ‘K’th key in a list, a simple iteration by specifying the condition is used.
Example
Below is a demonstration of the same
my_list = [{"Python": 2, "is": 4, "cool": 11}, {"Python": 5, "is": 1, "cool": 1}, {"Python": 7, "is": 3, "cool": 7}, {"Python": 9, "is": 9, "cool": 8}, {"Python": 4, "is": 10, "cool": 6}] print("The list is :") print(my_list) search_list = [1, 9, 8, 4, 5] key = "is" my_result = [] for sub in my_list: if sub[key] in search_list: my_result.append(sub) print("The result is :") print(my_result)
Output
The list is : [{'Python': 2, 'is': 4, 'cool': 11}, {'Python': 5, 'is': 1, 'cool': 1}, {'Python': 7, 'is': 3, 'cool': 7}, {'Python': 9, 'is': 9, 'cool': 8}, {'Python': 4, 'is': 10, 'cool': 6}] The result is : [{'Python': 2, 'is': 4, 'cool': 11}, {'Python': 5, 'is': 1, 'cool': 1}, {'Python': 9, 'is': 9, 'cool': 8}]
Explanation
A list of dictionary is defined and displayed on the console.
Another list of integers and a key are defined.
An empty list is defined.
The list is iterated and if the key is found, the element is appended to the emoty list.
This is the output.
It is displayed on the console.
- Related Articles
- Filter Tuples by Kth element from List in Python
- Python – Filter dictionaries with ordered values
- Python - Filter dictionary key based on the values in selective list
- Ways to sort list of dictionaries by values in Python
- Sort list of dictionaries by values in C#
- Ways to sort list of dictionaries by values in Python Using itemgetter
- Sort Python Dictionaries by Key or Value
- How to sort a list of dictionaries by values of dictionaries in C#?
- Ways to sort list of dictionaries by values in Python Using lambda function
- Ways to sort list of dictionaries using values in python
- How do I sort a list of dictionaries by values of the dictionary in Python?
- Python program to Sort a List of Dictionaries by the Sum of their Values
- Sort Dictionary key and values List in Python
- Python - Filter even values from a list
- Python – Replace value by Kth index value in Dictionary List

Advertisements