I have a list of columns and records that I get by using DATA-API RDS execute_statement using boto3. As the way the api responds it difficult to get the data out in a psycopg2 RealDictCursor format to insert into another database.
What I want to do is illustrated below.
columns = ["a","b","c"]
records = [[1,2,3],[4,5,6]]
I want to convert this to a dictionary that represents it like
[{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6}]
>Solution :
Do it like this:
Python 3.8.9 (default, Aug 3 2021, 19:21:54)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> columns = ["a","b","c"]
>>> records = [[1,2,3],[4,5,6]]
>>> [dict((a,b) for a,b in zip(columns,rec)) for rec in records]
[{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}]
>>>