I have code like this:
thing = thing.query.filter_by(id=thing_id).limit(1).all()[0]
all()[0]
feels a bit messy and redundant in the limit(1)
case. Is there a more terse (and/or otherwise optimal) way to achieve this?
There's first()
:
first()
applies a limit of one and returns the first result as a scalar
Thus:
thing = thing.query.filter_by(id=thing_id).first()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With