- 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
How to extract subset of key-value pairs from Python dictionary object?
Use dictionary comprehension technique.
We have dictionary object having name and percentage of students
>>> marks = { 'Ravi': 45.23, 'Amar': 62.78, 'Ishan': 20.55, 'Hema': 67.20, 'Balu': 90.75 }
To obtain dictionary of name and marks of students with percentage>50
>>> passed = { key:value for key, value in marks.items() if value > 50 } >>> passed {'Amar': 62.78, 'Hema': 67.2, 'Balu': 90.75}
To obtain subset of given names
>>> names = { 'Amar', 'Hema', 'Balu' } >>> lst = { key:value for key,value in marks.items() if key in names} >>> lst {'Amar': 62.78, 'Hema': 67.2, 'Balu': 90.75}
- Related Articles
- JavaScript: How to Create an Object from Key-Value Pairs
- Extract key value from a nested object in JavaScript?
- Python – Extract Key’s Value, if Key Present in List and Dictionary
- Get key from value in Dictionary in Python
- How to get a value for a given key from a Python dictionary?
- JavaScript: How to remove the key-value pairs corresponding to the given keys from an object?
- Get the number of key/value pairs in the Dictionary in C#
- How to remove a key from a python dictionary?
- Python Extract specific keys from dictionary?
- How to create Python dictionary from the value of another dictionary?
- Accessing Key-value in a Python Dictionary
- How to update the value of a key in a dictionary in Python?
- Add a key value pair to dictionary in Python
- Python - Ways to remove a key from dictionary
- How to print a value for a given key for Python dictionary?

Advertisements