
- 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 – Remove dictionary from a list of dictionaries if a particular value is not present
When it is required to remove dictionary from a list of dictionaries if a particular value is not present, a simple iteration and the ‘del’ operator is used.
Example
Below is a demonstration of the same −
my_list = [{"id" : 1, "data" : "Python"}, {"id" : 2, "data" : "Code"}, {"id" : 3, "data" : "Learn"}] print("The list is :") print(my_list) for index in range(len(my_list)): if my_list[index]['id'] == 2: del my_list[index] break print("The result is :") print(my_list)
Output
The list is : [{'id': 1, 'data': 'Python'}, {'id': 2, 'data': 'Code'}, {'id': 3, 'data': 'Learn'}] The result is : [{'id': 1, 'data': 'Python'}, {'id': 3, 'data': 'Learn'}]
Explanation
A list of dictionary elements is defined and is displayed on the console.
The list of dictionary is iterated over, and the ‘value’ associated with every key is checked to equivalent to 2.
If yes, that specific element is deleted.
The control breaks out of the loop.
In the end, this list of dictionary is displayed as output on the console.
- Related Questions & Answers
- Python Program – Remove dictionary from a list of dictionaries if a particular value is not present
- Python – Check if particular value is present corresponding to K key
- Python – Extract Key’s Value, if Key Present in List and Dictionary
- Python – All combinations of a Dictionary List
- Python – Check if any list element is present in Tuple
- Python – Create dictionary from the list
- Python – Remove Dictionaries with Matching Values
- Python Program – Create dictionary from the list
- Python – Dictionaries with Unique Value Lists
- Python – Convert List to Index and Value dictionary
- Remove tuple from list of tuples if not containing any character in Python
- Python – Replace value by Kth index value in Dictionary List
- How to remove a key from a python dictionary?
- How to remove a particular value from a vector in R?
- Python - Check if a List contain particular digits
Advertisements