-
-
Save russomi/0a16c5736c2f8af4034a09e0854a10c5 to your computer and use it in GitHub Desktop.
python filter a dictionary by keys or values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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