Last active
November 12, 2021 13:33
-
-
Save joseberlines/58bd7677bb760e00d86d8cf642ac124d to your computer and use it in GitHub Desktop.
three ways of performing lookup
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
# 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