Python: Create Dictionary using list of list, list of tuples and list
Question: I have a list `l1` as shown below:

    
    
    l1 = [[(1,"INC1"), (1, "INC2")],[(3, "INC4"),(4, "INC6")]]
    

I have a list `names` as shown below:

    
    
    names = ['John', 'Marcia']
    

I have another list `label_issue_list` as shown below:

    
    
    label_issue_list = [["Problem1","Problem1"],["Problem3","Problem4"]]
    

The indices of all the three lists are connected as shown in the below output.

I'm trying to use all these three lists and get a dictionary output which
would like the below:

    
    
    {
    "John" : {
                "Problem1" : 2,
              "tick_info" : {"1": ["INC1", "INC2"]}
    
                },
    "Marcia" : {
                "Problem3" : 1,
                "Problem4" : 1,
                "tick_info" : {"3":["INC4"], "4":["INC6"]}
               }
    }
    

I have tried the below code but this doesn't seem to work:

    
    
    clust_stack = {}
    for i in range(len(l1)):
          fq= defaultdict( list )
          for n,v in l1[i]:
               fq[str(n)].append(v)
          for name_, data in zip(names, label_issue_list)[i]:
               clust_stack[name_] = Counter(map(str,data))
               clust_stack[name_]["tick_info"] = {}
               clust_stack[name_]["tick_info"] = dict(fq)
    

I get the below error in the `for name_, data in zip(names,
label_issue_list)[i]:` line:

    
    
    ValueError: too many values to unpack
    

What do I need to do to get my desired output?


Answer: Start with zipping all 3 lists:

    
    
    clust_stack = {}
    for name, data, fq in zip(names, label_issue_list, l1):
        clust_stack[name] = dict(Counter(data))  # turn back into regular dictionary
        tick_info = clust_stack[name]['tick_info'] = {}
        for num, tick in fq:
            tick_info.setdefault(num, []).append(tick)
    

Using a `Counter` to count the information in each `l1` element is excellent,
but you do need to turn that result back to a regular dictionary so you can
add other keys to it.

You can then add the `tick_info` key to that, collecting the information in
those lists in list values per key.

Demo:

    
    
    >>> from collections import Counter
    >>> from pprint import pprint
    >>> l1 = [[(1,"INC1"), (1, "INC2")],[(3, "INC4"),(4, "INC6")]]
    >>> names = ['John', 'Marcia']
    >>> label_issue_list = [["Problem1","Problem1"],["Problem3","Problem4"]]
    >>> clust_stack = {}
    >>> for name, data, fq in zip(names, label_issue_list, l1):
    ...     clust_stack[name] = dict(Counter(data))  # turn back into regular dictionary
    ...     tick_info = clust_stack[name]['tick_info'] = {}
    ...     for num, tick in fq:
    ...         tick_info.setdefault(num, []).append(tick)
    ... 
    >>> pprint(clust_stack)
    {'John': {'Problem1': 2, 'tick_info': {1: ['INC1', 'INC2']}},
     'Marcia': {'Problem3': 1,
                'Problem4': 1,
                'tick_info': {3: ['INC4'], 4: ['INC6']}}}
    

