Skip to content

Instantly share code, notes, and snippets.

@spac3unit
Forked from 89465127/filter_dict.py
Created June 20, 2018 02:12
Show Gist options
  • Select an option

  • Save spac3unit/f4022d9f5d5abbec1843d4dd0d411293 to your computer and use it in GitHub Desktop.

Select an option

Save spac3unit/f4022d9f5d5abbec1843d4dd0d411293 to your computer and use it in GitHub Desktop.
python 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.iteritems())}
# filter by value
d3 = {k : v for k,v in d.iteritems() if k in [2,3]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment