Cheknov
Cheknov

Reputation: 2082

Python - How to convert a list in a list of dicts

I am using a custom MariaDB connector to retrieve some data from a DB.

Whenever I execute a SELECT query, like:

cursor.execute(SOME_SELECT_QUERY)

That returns an object with type: <class 'mariadb.connection.cursor'>

So I tried to convert it to a list with:

results = list(cursor)

So if I print the content of results, this is the result:

[mariadb.Row(record_id=440, chr='chr13'] [mariadb.Row(record_id=449, chr='chr13')]

And I would like to have something like this (a list of dicts):

[{'record_id': 440, 'chr': 'chr13'}, {'record_id': 449, 'chr': 'chr13'}]

Anyone knows how could I get the desired result in a pythonic way?

Upvotes: 1

Views: 186

Answers (1)

Aslı K&#246;k
Aslı K&#246;k

Reputation: 626

Did you try to pass dictionary=True while defining the cursor as below?

cursor = conn.cursor(dictionary=True)

Upvotes: 1

Related Questions