I have the following list of dicts:
[
   {"taskid": 1, "type": "input", "name": "First_in"},
   {"taskid": 1, "type": "input", "name": "Second_in"},
   {"taskid": 1, "type": "input", "name": "Third_in"},
   {"taskid": 1, "type": "output", "name": "First_out"},
   {"taskid": 1, "type": "output", "name": "Second_out"},
   {"taskid": 1, "type": "output", "name": "Third_out"},
   {"taskid": 2, "type": "input", "name": "First_in"},
   {"taskid": 2, "type": "output", "name": "First_out"},
   {"taskid": 2, "type": "output", "name": "Second_out"},
...]
And I need to restructure it to obtain the following result:
[
   {"taskid": 1, 
    "input": ["First_in", "Second_in", "Third_in"], 
    "output": ["First_out", "Second_out", "Third_out"]
   },
   {"taskid": 2, 
    "input": ["First_in"], 
    "output": ["First_out","Second_out"]
   },
...]
Here is my code for this:
def squash_records(rec):
    squashed = []
    # get all taskids
    tasks = []
    for item in rec:
        tasks.append(item['taskid'])
    for task in tasks:
        current_task = {}
        current_task['taskid'] = task
        current_task['input'] = [row['name'] for row in rec if row['type'] == 'input' and row['taskid'] == task]
        current_task['output'] = [row['name'] for row in rec if row['type'] == 'output' and row['taskid'] == task]
        squashed.append(current_task)
    return squashed
Which is the best way to implement it if this array is a generator? I mean - for single for ... loop ?
Thank you in advance!
 
     
    