This short Python snippet shows you how to filter a dictionary against a list of values via the use of a list comprehension.
>>> d = {"apples":1,"bananas":2,"pears":3}
>>> {x:y for x,y in d.iteritems() if x in ["apples","pears"]}
{'apples': 1, 'pears': 3}
Latest posts by Rick Donato (see all)
- Fortinet– How to configure NTP on FortiGate - January 13, 2026
- How to Configure a BIND Server on Ubuntu - March 15, 2018
- What is a BGP Confederation? - March 6, 2018
Want to become a programming expert ?
Here is our hand-picked selection of the best courses you can find online:
Python Zero to Hero course
Python Pro Bootcamp
Bash Scripting and Shell Programming course
Automate with Shell Scripting course
The Complete Web Development Bootcamp course
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial