Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy - query.all() - list of tuples to dict

This is my call: session.query(User.username, User.first_name, User.last_name).all()

It returns: [('myUsername', 'myFirstname', 'myLastname')].

I want that to be the following format:
[{"username":"myUsername", "first_name":"myFirstname", "last_name":"myLastname"}]

I tried with [dict(zip(["username","first_name","lastname"], x))]
But it returns this: [{'username': ('myUsername', 'myFirstname', 'myLastname')}]

like image 385
zPrima Avatar asked Feb 14 '23 13:02

zPrima


1 Answers

row = session.query(User.username, User.first_name, User.last_name).all()
as_dict = dict(zip(row.keys(), row))
like image 112
Burhan Khalid Avatar answered Feb 16 '23 02:02

Burhan Khalid