Skip to content

Instantly share code, notes, and snippets.

@askeing
Last active June 15, 2017 07:01
Show Gist options
  • Save askeing/d80137f8288d64a9bdbd9832c1b335c0 to your computer and use it in GitHub Desktop.
Save askeing/d80137f8288d64a9bdbd9832c1b335c0 to your computer and use it in GitHub Desktop.
>>> # Soultion 1
...
>>> data = {
... "id-1": {
... "p": "w",
... "t": "foo",
... "b": "f"
... },
... "id-2": {
... "p": "w",
... "t": "bar",
... "b": "c"
... },
... "id-3": {
... "p": "m",
... "t": "bar",
... "b": "f"
... }
... }
>>>
>>> [k for k, v in data.items() if v.get('p') == 'w']
['id-1', 'id-2']
>>> [k for k, v in data.items() if v.get('p') == 'm']
['id-3']
>>> [k for k, v in data.items() if v.get('t') == 'foo']
['id-1']
>>> [k for k, v in data.items() if v.get('p') == 'w' and v.get('b') == 'f']
['id-1']
>>>
>>>
>>> # Soultion 2
...
>>>
>>> list_data = {
... "id-1": ["w", "foo", "f"],
... "id-2": ["w", "bar", "c"],
... "id-3": ["m", "bar", "f"]
... }
>>> [k for k, v in list_data.items() if 'w' in v]
['id-1', 'id-2']
>>> [k for k, v in list_data.items() if 'w' in v and 'bar' in v]
['id-2']
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment