fir3net

PPS-Firenetbanner-780.5x190-30-03-17

Python - Filtering a dictionary against a list of values

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}

Tags: Python