I have the following list and dict:
external_list = ["122479-6-995220", "122479-2-112234", "223344-1-434312", "223344-3-575342", "223344-0-092312", "223344-4-215452", "338855-5-828822", "338855-7-234567", "338855-8-000000", "440099-9-111111"]
internal_list = ["11", "12", "13", "14", "15", "16", "17", "18", "19", "F"]
dict = {
"122479-6-995220": "11",
"122479-2-112234": "11",
"223344-1-434312": "12",
"223344-3-575342": "13",
"223344-0-092312": "14",
"223344-4-215452": "16",
"338855-5-828822": "16",
"338855-7-234567": "16",
"338855-8-000000": "F",
"440099-9-111111": "F"
}
I want to get this nested dict:
updated_dict = {
"122479-6-995220": { "11": 1, "12": 0, "13": 0, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 0},
"122479-2-112234": { "11": 1, "12": 0, "13": 0, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 0},
"223344-1-434312": { "11": 0, "12": 1, "13": 0, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 0},
"223344-3-575342": { "11": 0, "12": 0, "13": 1, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 0},
"223344-0-092312": { "11": 0, "12": 0, "13": 0, "14": 1, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 0},
"223344-4-215452": { "11": 0, "12": 0, "13": 0, "14": 0, "15": 0, "16": 1, "17": 0, "18": 0, "19": 0, "F": 0},
"338855-5-828822": { "11": 0, "12": 0, "13": 0, "14": 0, "15": 0, "16": 1, "17": 0, "18": 0, "19": 0, "F": 0},
"338855-7-234567": { "11": 0, "12": 0, "13": 0, "14": 0, "15": 0, "16": 1, "17": 0, "18": 0, "19": 0, "F": 0},
"338855-8-000000": { "11": 0, "12": 0, "13": 0, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 1},
"440099-9-111111": { "11": 0, "12": 0, "13": 0, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 1},
}
Basically i add to each internal list of keys all the elements from the list with values set to zero if they weren't already there, else 1.
A solution that creates a brand new dictionary would also be good.
Edit:
Before i edited the post i used another dictionary structure, same lists:
dict = {
"122479-6-995220": { "11": 1},
"122479-2-112234": { "11": 1},
"223344-1-434312": { "12": 1},
"223344-3-575342": { "13": 1},
"223344-0-092312": { "14": 1},
"223344-4-215452": { "16": 1},
"338855-5-828822": { "16": 1},
"338855-7-234567": { "16": 1},
"338855-8-000000": { "F": 1},
"440099-9-111111": { "F": 1},
}
The solution posted by metatoaster refers to this
question from:
https://stackoverflow.com/questions/65844470/creating-a-nested-dictionary-by-adding-elements-from-a-list