Search code examples
pythonpython-3.xlist-comprehensiondictionary-comprehension

List comprehension to dictionary with multiple commands (Python, curser.execute)?


I work with Python 3.6. I have connected to Hive using Pyhive and my cursor is cur.

Objective: Build a databases - tables map in a dictionary: dbname1 : list of tables within this db.

I have built it using for loop but I would like to transfer it to a dictionary comprehension please tell me what is wrong with my dict comprehension:

lst = extract_dbs_tbls(cur, s = 'show databases')

map_tbl_db = {}

for i in lst:
    cur.execute("use %s" % i)
    map_tbl_db['i'] = extract_dbs_tbls(cur, 'show tables')

Note: lst is a list of databases names like ['default', 'dwh', ...]

Basically what I want is the following dict comprehension:

{i:j for i in lst cur.execute('use %i' % i) j = extract_dbs_tbls(cur, s = 'show tables')}

Where extract_dbs_tbls:

def extract_dbs_tbls(cur, s = 'show tables'):

    "Return list of dbs / tables. Note: use before 'use db_name'."

    cur.execute(s)
    lst = cur.fetchall()

    return [j[0] for j in lst];

Solution

  • To call cur.execute as additional operation in list comprehension you can use some dummy var and list (cur.execute is placed in list [ ] to create iterable with one element just to call contained function) that will not be used in result dict. And your j var is not needed and replaced by in place calling extract_dbs_tbls:

    {i: extract_dbs_tbls(cur, s = 'show tables')
     for i in lst
     for dummy in [cur.execute('use %i' % i)]}
    

    above is equivalent to your loop:

    map_tbl_db = {}
    for i in lst:
        cur.execute("use %s" % i)
        map_tbl_db[i] = extract_dbs_tbls(cur, 'show tables')