Skip to content

Instantly share code, notes, and snippets.

@joseberlines
Last active November 12, 2021 13:33
Show Gist options
  • Save joseberlines/58bd7677bb760e00d86d8cf642ac124d to your computer and use it in GitHub Desktop.
Save joseberlines/58bd7677bb760e00d86d8cf642ac124d to your computer and use it in GitHub Desktop.
three ways of performing lookup
# THE DATA
# L is a list of dicts (all have the same keys)
L=[{'name':'e'+str(i),
'explanation':'objection1',
'law':(),
'CS':('barcelona','Vigo')} for i in range(1000000)]
# option 1: generator
next(item for item in L if item["name"] == myvalue)
# option 2: comprehension
[d for d in L if d["name"] == myvalue]
# option 3: pandas
df = pd.DataFrame(L).set_index('name')
df.loc[[myvalue]].reset_index().to_dict('records')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment