SQL Alchemy ORM returning a single column, how to avoid common post processing
Solution 1:
One way to decrease the clutter in the source is to iterate like this:
results = [r for (r, ) in results]
Although this solution is one character longer than using the []
operator, I think it's easier on the eyes.
For even less clutter, remove the parenthesis. This makes it harder when reading the code, to notice that you're actually handling tuples, though:
results = [r for r, in results]
Solution 2:
Python's zip combined with the * inline expansion operator is a pretty handy solution to this:
>>> results = [('result',), ('result_2',), ('result_3',)]
>>> zip(*results)
[('result', 'result_2', 'result_3')]
Then you only have to [0] index in once. For such a short list your comprehension is faster:
>>> timeit('result = zip(*[("result",), ("result_2",), ("result_3",)])', number=10000)
0.010490894317626953
>>> timeit('result = [ result[0] for result in [("result",), ("result_2",), ("result_3",)] ]', number=10000)
0.0028390884399414062
However for longer lists zip should be faster:
>>> timeit('result = zip(*[(1,)]*100)', number=10000)
0.049577951431274414
>>> timeit('result = [ result[0] for result in [(1,)]*100 ]', number=10000)
0.11178708076477051
So it's up to you to determine which is better for your situation.
Solution 3:
I struggled with this too until I realized it's just like any other query:
for result in results:
print result.column_name
Solution 4:
I found the following more readable, also includes the answer for the dict (in Python 2.7):
d = {id_: name for id_, name in session.query(Customer.id, Customer.name).all()}
l = [r.id for r in session.query(Customer).all()]
For the single value, borrowing from another answer:
l = [name for (name, ) in session.query(Customer.name).all()]
Compare with the built-in zip
solution, adapted to the list:
l = list(zip(*session.query(Customer.id).all())[0])
which in my timeits provides only about 4% speed improvements.