1

This problem is similar to another where I learned How to Create Nested Dictionary in Python with 3 lists

How can I achieve an output such from 3 lists that takes the following output form

a = ['A', 'B', 'C', 'D']
b = [1,2,3,4]
c = ['N1', 'N2', 'N3', N4]

output = [{'A': {'N1':1}}, {'B':{'N2':2}}, {'C':{'N3':3}}, {'D':{'N4':4}}]

The output is different than on the posted link. If I could describe this I would say it is a dictionary where 'N#' are the keywords for the values 1-4, and in turn those dictionaries have keyword 'A'-'D', which all seem to be dictionaries insided some top level keyword not shown under which they all are the elements of that master keyword.

I used this line of code which provided an output. These are different values than I'm using, but the point is this doesn't have as many curly brackets or the square brackets at the edges

d = {k: {x: y} for k, x, y in zip(a, b, c)}

# output: {'A':{'1' :'9'} , 'B':{'2':'8'}, 'C':{'3':'7'} , 'D':{'4':'6'}}

So I've explained what I think it means but I'm not sure how to proceed. I tried doing something like

d = {w:{k:{x:y}}for w,k,x,y in zip(sound, a, b, c)}

where 'sound' is a list of the same element value but that just prints out the 4th things on the list. Any help to clear this up would be much appreciated. Thanks.

2 Answers2

3

Use a list comprehension. Iterate over the zipped lists and create the nested dictionaries:

a = ['A', 'B', 'C', 'D']
b = [1, 2, 3, 4]
c = ['N1', 'N2', 'N3', 'N4']

>>> [{k1: {k2: v}} for k1, v, k2 in zip(a, b, c)]
[{'A': {'N1': 1}}, {'B': {'N2': 2}}, {'C': {'N3': 3}}, {'D': {'N4': 4}}]
flakes
  • 21,558
  • 8
  • 41
  • 88
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • Cool, Thanks. I'm going to read into list comprehension. Its a list of nested dictionaries. – Hackem2bits Jan 26 '21 at 07:43
  • @Hackem2bits: in this case, yes, it is a list of dictionaries. But a list can contain any object and list comprehensions can be used to construct those too. – mhawke Jan 26 '21 at 11:47
0

The dict comprehension is good by @mhawke, but let me add a more naive solution that may need to understand how to implement this type of nested Dictionary:

a = ['A', 'B', 'C', 'D']
b = [1, 2, 3, 4]
c = ['N1', 'N2', 'N3', 'N4']

output = [{'A': {'N1':1}}, {'B':{'N2':2}}, {'C':{'N3':3}}, {'D':{'N4':4}}]

def create_nested(a, b, c):

    result = []
    for i in range(len(a)):
        parent = {}
        child = {c[i]: b[i]}
        parent.setdefault(a[i], child)
        result.append(parent)
    return result


result = create_nested(a, b, c)
print(result)
assert result == output

Using enumerate

def create_nested(a, b, c):

    result = []
    
    for idx, value in enumerate(a):
        child = {c[idx]: b[idx]}
        parent = {a[idx]: child}
        result.append(parent)
    return result 

Shorter form

for idx, value in enumerate(a):
        parent = {a[idx]: {c[idx]: b[idx]}}
        result.append(parent)
    return result 
Federico Baù
  • 6,013
  • 5
  • 30
  • 38