Turning a list of list of list into a single dictionary

How do I get a dictionary like this:

{'SEA': {'SFO': 6.020797289396148, 'DEN': 12.041594578792296, 'OAK': 6.029925372672534}, 'SFO': {'SEA': 6.020797289396148, 'DEN': 9.7082439194738}}

from something like this:

[['SEA', ['SFO', 6.020797289396148, 'DEN', 12.041594578792296, 'OAK', 6.029925372672534]], ['SFO', ['SEA', 6.020797289396148, 'DEN', 9.7082439194738]]]

4 answers

  • answered 2018-11-08 06:37 Sandeep Kadapa

    Use a nested dictionary comprehension with zip and dict as:

    l = [['SEA', ['SFO', 6.020797289396148, 'DEN', 12.041594578792296, 'OAK', 6.029925372672534]], ['SFO', ['SEA', 6.020797289396148, 'DEN', 9.7082439194738]]]
    d = {v[0]:{k:v1 for k,v1 in zip(v[1][::2], v[1][1::2])} for v in l}
    

    Or using a for loop like so:

    d = {}
    for v in l:
        temp_d = {}
        for k,v1 in zip(v[1][::2],v[1][1::2]):
            temp_d[k]=v1
        d[v[0]]=temp_d
    

    Or:

    d = {v[0]:dict(zip(v[1][::2],v[1][1::2])) for v in l}
    

    print(d)
    {'SEA': {'SFO': 6.020797289396148,
      'DEN': 12.041594578792296,
      'OAK': 6.029925372672534},
     'SFO': {'SEA': 6.020797289396148, 'DEN': 9.7082439194738}}
    

  • answered 2018-11-08 06:44 Rahul K P

    You can do with dict comprehension,

    In [95]: {key:dict(zip(values[::2], values[1::2])) for key,values in a}
    Out[95]: 
    {'SEA': {'DEN': 12.041594578792296,
      'OAK': 6.029925372672534,
      'SFO': 6.020797289396148},
     'SFO': {'DEN': 9.7082439194738, 'SEA': 6.020797289396148}}
    

    zip(values[::2], values[1::2]) will create list of tuples like this,

    [('SFO', 6.020797289396148),
      ('DEN', 12.041594578792296),
      ('OAK', 6.029925372672534)] 
    

    dict(zip(values[::2], values[1::2])) will generate a dictionary liks this,

    {'DEN': 12.041594578792296,
      'OAK': 6.029925372672534,
      'SFO': 6.020797289396148}
    

  • answered 2018-11-08 06:46 Subhaditya Mukherjee

    Simple way of doing it

    d = {'SEA': {'SFO': 6.020797289396148, 'DEN': 12.041594578792296, 'OAK': 6.029925372672534}, 'SFO': {'SEA': 6.020797289396148, 'DEN': 9.7082439194738}}
    
    l = []
    for a in d.keys():
        l2 = []
        d2 = d[a]
        for b in d2.keys():
            l2.extend([b,d2[b]])
        l.extend([a,l2])
    print(l)
    

    You get ['SEA', ['SFO', 6.020797289396148, 'DEN', 12.041594578792296, 'OAK', 6.029925372672534], 'SFO', ['SEA', 6.020797289396148, 'DEN', 9.7082439194738]]

  • answered 2018-11-08 06:46 Suyambu Education

    [['SEA', ['SFO', 6.020797289396148, 'DEN', 12.041594578792296, 'OAK', 6.029925372672534]], ['SFO', ['SEA', 6.020797289396148, 'DEN', 9.7082439194738]]]
    

    In this on showing Array of Array which means [ '1',['a',['a1','a2','a3']] ] The main Arrya 0th index ['1'] is a normal value and the another index is a Array ['a',['a1','a2','a3']]

    But In Second Case it's a JSON data

    {'SEA': {'SFO': 6.020797289396148, 'DEN': 12.041594578792296, 'OAK': 6.029925372672534}, 'SFO': {'SEA': 6.020797289396148, 'DEN': 9.7082439194738}}
    

    Example :

    { "SEA" :{'SFO':6.020797289396148}}
    

    SEA -> {} - SFO -> 6.020797289396148 basically the second method contain a object of object the object value as a object in that object contain the value 6.020797289396148

    SEA.SFO using this you can get the value of this 6.020797289396148