Created
November 14, 2018 08:10
-
-
Save BbsonLin/b4dd32191b30585dc7f20ae06759011f to your computer and use it in GitHub Desktop.
How to filter dictionary key or value by list
This file contains hidden or 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 = {'Bobson': 20, 'Bob': 22, 'Lin': 33} | |
# filter by key | |
d2 = {k : v for k,v in filter(lambda t: t[0] in ['Bobson', 'Lin'], d.items())} | |
print('d2 = ', d2) | |
# filter by value | |
d3 = {k : v for k,v in d.items() if v in [22, 33]} | |
print('d3 = ', d3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment