How can I correct my code to produce a nested dictionary?
Question: I am trying to create a nested dictionary. I have a list of tuples (called
'kinetic_parameters') which looks like this:

    
    
    ('New Model','v7','k1',0.1)
    ('New Model','v8','k2',0.2)
    ('New Model','v8','k3',0.3)
    

I need the second column to be the outer key and the value to be another
dictionary with inner key being the third column and value being the number in
the fourth.

I currently have:

    
    
        for i in kinetic_parameters:
            dict[i[1]]={}
            dict[i[1]][i[2]]=i[3]
    

But this code will not deal with multiple keys in the inner dictionary so I
lose some information. Does anybody know how to correct my problem?

I'm using Python 2.7 and I want the output to look like this:

    
    
    {'v7': {'k1': 0.1}, 'v8':{'k2':0.2, 'k3': 0.3}}
    


Answer: Use a
[defaultdict](https://docs.python.org/2/library/collections.html#collections.defaultdict),
and don't use `dict` as a variable name, since we need it to refer to the
dictionary type:

    
    
    import collections
    d = collections.defaultdict(dict)
    for i in kinetic_parameters:
        d[i[1]][i[2]]=i[3]
    

This will create the dictionaries automatically.

