python convert list of lists into dictionary

Question:

I have a structure that consists of a list of lists. I would like to convert it to a dictionary where the array of size one are the keys and the subsequent items in the array where array are size 2 are the key – value of the key. Every item in the array of size 1 will be a new key.

For example:

list of lists:

[
    [
        "key1"
    ],
    [
        "name1",
        "value1"
    ],
    [
        "name2",
        "value2"
    ],
    [
        "key2"
    ],
    [
        "name1",
        "value1"
    ],
    [
        "name2",
        "value2"
    ],
    [
        "key3"
    ],
    [
        "name1",
        "value1"
    ],
    [
        "name2",
        "value2"
    ],
    [
        "key4"
    ],
    [
        "name1",
        "value1"
    ],
    [
        "name2",
        "value2"
    ]
]

desired dictionary conversion:

{
    "key1": {
        "name1": "value1",
        "name2": "value2"
    },
    "key2": {
        "name1": "value1",
        "name2": "value2"
    },
    "key3": {
        "name1": "value1",
        "name2": "value2"
    },
    "key4": {
        "name1": "value1",
        "name2": "value2"
    }
}
Asked By: rf guy

||

Answers:

So the first thing you want is a nice way to turn this list into a list of pairs.

There’s a canonical solution, given in this answer

(That’s a little obscure and hard to read if you’re not used to that notation – I’m not going to go into it here, but it’s worth digging into why it works!)

So you can define a function to convert your list into pairs, like so:

def pairs(it):
    return zip(it[::2], it[1::2])

Now you just need a dict comprehension, bearing in mind that you need to extract the key element from the list-of-one that it’s currently in:

result = {key[0]: val for key, val in pairs(input_list)}
Answered By: Jon Kiparsky

This is a solution that I thought of and could be a solution to your problem:

import json

myList = [
    [
        "key1"
    ],
    [
        "name1",
        "value1"
    ],
    [
        "name2",
        "value2"
    ],
    [
        "key2"
    ],
    [
        "name1",
        "value1"
    ],
    [
        "name2",
        "value2"
    ],
    [
        "key3"
    ],
    [
        "name1",
        "value1"
    ],
    [
        "name2",
        "value2"
    ],
    [
        "key4"
    ],
    [
        "name1",
        "value1"
    ],
    [
        "name2",
        "value2"
    ]
]

key = 0
value = 0
myDict = dict()

for item in myList:
    if len(item) == 1: 
        myDict[item[0]] = dict()
        key = myList.index(item)
    if len(item) == 2 and value == 0:
        myDict[myList[key][0]][item[0]] = item[1]
    if len(item) == 2 and value == 1:
        myDict[myList[key][0]][item[0]] = item[1]
        value = 0

print(json.dumps(myDict, indent=4))

which outputs:

{
    "key1": {
        "name1": "value1",
        "name2": "value2"
    },
    "key2": {
        "name1": "value1",
        "name2": "value2"
    },
    "key3": {
        "name1": "value1",
        "name2": "value2"
    },
    "key4": {
        "name1": "value1",
        "name2": "value2"
    }
}
Answered By: m2r105