Skip to content

Instantly share code, notes, and snippets.

@BbsonLin
Created November 14, 2018 08:10
Show Gist options
  • Save BbsonLin/b4dd32191b30585dc7f20ae06759011f to your computer and use it in GitHub Desktop.
Save BbsonLin/b4dd32191b30585dc7f20ae06759011f to your computer and use it in GitHub Desktop.
How to filter dictionary key or value by list
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