Search code examples
pythonpsycopg2aws-rds-data-service

Add list as keys for another list and convert to dictionary


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(zip(columns,rec)) for rec in records]
    [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}]
    >>> 
    

    How it works

    Work from the outside in. [...] means we'll produce a list. x for rec in records means we will evaluate the part in x once for every element in records. zip(columns,rec) zips together the column names and rec, which will be an element from records. So, zip(['a','b','c'],[1,2,3]) produces the list ('a',1), ('b',2), ('c',3), which are the things we want to build the dict from. And, if you pass a list of 2-tuples to the dictionary constructor, it is happy to create a dictionary from it. ('a',1) becomes {'a':1,...}