How to create a dictionary out of weird list format?

Viewed 50

So I used SQL to try to describe a table and got a weird output because my function returns a list so I have this weird format that I want to make into a list of dictionaries. But not sure how I would go about it. And was wondering if someone could point me in the right direction. So here is the list I have

[['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'],
(('_id', 'bigint(20) unsigned', 'NO', 'PRI', None, ''), 
('_load_dt', 'date', 'NO', '', None, ''), 
('_load_dt_time', 'timestamp', 'YES', 'MUL', 'current_timestamp()', ''), 
('_data_hash', 'char(160)', 'YES', 'UNI', None, ''), 
('_host', 'char(200)', 'YES', '', None, ''), 
('_port', 'int(6)', 'YES', '', None, ''), 
('_schema', 'char(200)', 'YES', '', None, ''), 
('_deleted', 'tinyint(1)', 'YES', '', '0', ''), 
('acct_id', 'varchar(200)', 'NO', 'MUL', None, ''), 
('account_title', 'varchar(200)', 'NO', 'MUL', None, ''), 
('signup_date', 'varchar(200)', 'NO', 'MUL', None, ''), 
('admin_email', 'varchar(200)', 'NO', 'MUL', None, ''))]

I want to the end result to look something like this [{Field:_id, Type:bigint(20) unsigned, Null:No, Key:PRI, Extra:None}, {Field:_load_dt, Type:date, NULL:No, Key:'', Default:None, Extra:''}, .....]

I'm not sure where to begin, maybe have two loops one that creates the keys by looping through just the first bracket and then another loop that loops through each position in parentheses to get the value for each of those keys?

3 Answers

Try this:

# This is the info you provided in the original question:
data = [['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'],
(('_id', 'bigint(20) unsigned', 'NO', 'PRI', None, ''), 
('_load_dt', 'date', 'NO', '', None, ''), 
('_load_dt_time', 'timestamp', 'YES', 'MUL', 'current_timestamp()', ''), 
('_data_hash', 'char(160)', 'YES', 'UNI', None, ''), 
('_host', 'char(200)', 'YES', '', None, ''), 
('_port', 'int(6)', 'YES', '', None, ''), 
('_schema', 'char(200)', 'YES', '', None, ''), 
('_deleted', 'tinyint(1)', 'YES', '', '0', ''), 
('acct_id', 'varchar(200)', 'NO', 'MUL', None, ''), 
('account_title', 'varchar(200)', 'NO', 'MUL', None, ''), 
('signup_date', 'varchar(200)', 'NO', 'MUL', None, ''), 
('admin_email', 'varchar(200)', 'NO', 'MUL', None, ''))]

# Separate the column definitions and the items themselves:
(cols, items) = data

# Use a list comprehension to create a new list that looks the way you expect:
result = [dict(zip(cols, item)) for item in items]

# Done
print(result)

For more info, check out:

simple solution

In [2]: data = [['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'], 
   ...: (('_id', 'bigint(20) unsigned', 'NO', 'PRI', None, ''),  
   ...: ('_load_dt', 'date', 'NO', '', None, ''),  
   ...: ('_load_dt_time', 'timestamp', 'YES', 'MUL', 'current_timestamp()', ''),  
   ...: ('_data_hash', 'char(160)', 'YES', 'UNI', None, ''),  
   ...: ('_host', 'char(200)', 'YES', '', None, ''),  
   ...: ('_port', 'int(6)', 'YES', '', None, ''),  
   ...: ('_schema', 'char(200)', 'YES', '', None, ''),  
   ...: ('_deleted', 'tinyint(1)', 'YES', '', '0', ''),  
   ...: ('acct_id', 'varchar(200)', 'NO', 'MUL', None, ''),  
   ...: ('account_title', 'varchar(200)', 'NO', 'MUL', None, ''),  
   ...: ('signup_date', 'varchar(200)', 'NO', 'MUL', None, ''),  
   ...: ('admin_email', 'varchar(200)', 'NO', 'MUL', None, ''))] 

In [5]: fields = data[0]                                                                                                                               

In [6]: res = []                                                                                                                                       

In [7]: items = data[1] 

In [13]: for item in items: 
    ...:     resItem = {} 
    ...:     for index,fieldItem in enumerate(fields): 
    ...:         resItem[fieldItem] = item[index] 
    ...:     res.append(resItem) 

In [14]: res                                                                                                                                           

One thing you can do is import pandas and do df = pandas.DataFrame(data[1], columns = data[0]). This will turn your data into a dataframe, which likely will be more useful than a list of dictionaries. It's not too difficult to get a list of dictionaries out of a dataframe, such as with df.to_dict('records'). If you want to just get a list of dictionaries directly, you can do list_of_dicts = [{key: value for key, value in zip(data[0], row)} for row in data[1]].

Related