So take the following example list
l = [
{
'post':1,
'user':1,
'other_stuff':'something',
'more':'you get the point'
},
{
'post':1,
'user':2,
'other_stuff':'something',
'more':'you get the point'
},
{
'post':2,
'user':1,
'other_stuff':'something',
'more':'you get the point'
},
]
I need to be able to check if a 'user' is already connected to a 'post', and I could do it with looping:
user = 1
post = 1
response = False
for connection in l:
if connection['post'] == post and connection['user'] == user:
response = True
break
and that works very well. The issue is that in the actual situation, l will be populated 1.5 million times, and this iteration will run every time it populates since it needs to check to see if something already exists. so the last 500k iterations will be iterating through a list of over 1 million dictionaries. There is no way that this is the most efficient method for this!! My question is: what would be an optimal method that would not require such exhaust?
note: I do not necessarily know the values of the other keys in the dictionaries so I cannot do if x is in l to check
lvalues come in? Then you can run queries against the database without needing to iterate over all the rows yourself.