Search code examples
pythonlistdictionarytupleskey-value-coding

How to form a dictionary from list of tuples?


I have a list of tuple such as:

iList = [('FirstParam', 1), ('FirstParam', 2), ('FirstParam', 3), ('FirstParam', 4), ('SecondParam', 5), ('SecondParam', 6), ('SecondParam', 7)]

I want to make a dictionary which should look like:

iDict = {'FirstParam': 1, 'SecondParam': 5}{'FirstParam': 1, 'SecondParam': 6}{'FirstParam': 1, 'SecondParam': 7}{'FirstParam': 2, 'SecondParam': 5}{'FirstParam': 2, 'SecondParam': 6}{'FirstParam': 2, 'SecondParam': 7}{'FirstParam': 3, 'SecondParam': 5}{'FirstParam': 3, 'SecondParam': 6}{'FirstParam': 3, 'SecondParam': 7}{'FirstParam': 4, 'SecondParam': 5}{'FirstParam': 4, 'SecondParam': 6}{'FirstParam': 4, 'SecondParam': 7}

so iDict forms all the possible combinations from iList.

MyExp will be the key for the dictionary which I'm going to form. Hence in the end it should be

Dictionary = dict(itertools.izip(MyExp, iDict))

I'm trying to generate iDict first and I tried

h = {}
[h.update({k:v}) for k,v in iList]
print "Partial:", h

I was hoping to get

Partial: {{'FirstParam': 1}, {'FirstParam': 2}, {'FirstParam': 3}, {'FirstParam': 4}{'SecondParam': 5}, {'SecondParam': 6}, {'SecondParam': 7}}

from where I would have proceeded ahead to get actual iDict and then Dictionary. But instead I got the following output.

Partial: {'FirstParam': 4, 'SecondParam': 7}

Can anyone tell me where exactly my logic went wrong and how should I proceed further?


Solution

  • iDict isn't going to be a dictionary. It can't, as the keys are repeated. By definition, dictionaries have unique keys. Instead, I'm going to guess that you really want iDict to be a list of dictionaries, with every combination of 'FirstParam' and 'SecondParam' represented as one of the dictionaries.

    So first, we're going to want to break up your list of tuples into two lists, one with all the 'FirstParam' tuples and one with all the 'SecondParam'.

    iList = [('FirstParam', 1), ('FirstParam', 2), 
             ('FirstParam', 3), ('FirstParam', 4), 
             ('SecondParam', 5), ('SecondParam', 6), 
             ('SecondParam', 7)]
    
    first_params = [i for i in iList if i[0] == 'FirstParam']
    second_params = [i for i in iList if i[0] == 'SecondParam']
    

    Now we need to take every combination of these two lists and form a dictionary out of them, and then put those dictionaries in a list. We can do all of this in one statement, using itertools.product to take all the combinations of parameters, converting the tuples that product returns into dictionaries using dict, and doing it for all the combinations with a list comprehension.

    from itertools import product
    
    result = [dict(tup) for tup in product(first_params, second_params)]
    
    print(result)
    # [{'FirstParam': 1, 'SecondParam': 5},
    #  {'FirstParam': 1, 'SecondParam': 6},
    #  {'FirstParam': 1, 'SecondParam': 7},
    #  {'FirstParam': 2, 'SecondParam': 5},
    #  {'FirstParam': 2, 'SecondParam': 6},
    #  {'FirstParam': 2, 'SecondParam': 7},
    #  {'FirstParam': 3, 'SecondParam': 5},
    #  {'FirstParam': 3, 'SecondParam': 6},
    #  {'FirstParam': 3, 'SecondParam': 7},
    #  {'FirstParam': 4, 'SecondParam': 5},
    #  {'FirstParam': 4, 'SecondParam': 6},
    #  {'FirstParam': 4, 'SecondParam': 7}]