Not sure if this is possible or not, but I’m using sqlalchemy and my queries returns all the items as objects. I want to search for items within that list of objects but not have to do a for loop each time.
Here’s how I know now to currently do it:
for x in objects_from_query:
print x.name, ' - ', x.age
if I want to find out if I have data for a user named ‘bob’ in my list I would have to:
for x in objects_from_query:
if x.name == 'bob':
print 'bob exists!'
Because I have to do this a lot, I’m wondering if there’s a faster way to find if bob exists without having to do a for loop every time? Typically with lists I do something like objects_from_query.index(“bob”) but is there something similar when instead of a normal list, its a list of objects?
If you’re using sqlalchemy, you can use the
.filtermethod on the Query object, which translates to SQL as a where clause. Something like the following would work:-