Skip to content

Instantly share code, notes, and snippets.

@mazhar266
Forked from 89465127/filter_dict.py
Last active August 9, 2018 12:41
Show Gist options
  • Save mazhar266/820948f858ab2ca7f52f02a6f200f142 to your computer and use it in GitHub Desktop.
Save mazhar266/820948f858ab2ca7f52f02a6f200f142 to your computer and use it in GitHub Desktop.
python 2 & 3 filter a dictionary by keys or values
d = {1:11, 2:22, 3:33}
# filter by key
d2 = {k: v for k, v in filter(lambda t: t[0] in [1, 3], d.items())}
# filter by value
d3 = {k: v for k, v in d.items() if v in [2,3]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment