Mapping four string values to all different possibilities of two number ranges in a nested dictionary in python

Question:

I have the following lists. In reality these lists are a lot bigger but I simplified for the example.

d = ["a", "b", "c", "d"]
p = [1,2,3,4]
a = [1,2,3,4]

I need to map the letters in d to all different possibilities of p*a in the following format:

{0: {'a': {p:1, a:1}
         'b': {p:1, a:1}
         'c': {p:1, a:1}
         'd': {p:1, a:1}
         }}

This could also be a list instead of a dictionary where the index is the possibility and where the n of the list is all possible combinations of the letters and the dictionaries of p and a

The code that I have written looks like this

p = [1,2,3,4]
a = [1,2,3,4]
d = ["a", "b", "c", "d"]

o = list()
end_dict = list()
for y in p:
    for z in a:
        o.append({"p":y, "a":z})

counter = 0
for x in d:
    for y in o:
        print(counter)
        end_dict.append({x:y})
        counter+=1
print(end_dict)

This however does not produce a list that has all the letters in a dictionary together. I have no clue how to get that done efficiently.

This is code that should work I believe:

    p = [1,2,3,4]
    a = [1,2,3,4]
    d = ["a", "b", "c", "d"]

    o = list()
    end_dict = list()
    for price in p:
        for amount in a:
            o.append({"p": price, "a": amount})

    for w in o:
        for x in o:
            for y in o:
                for z in o:
                    test = {
                        d[0]:w,
                        d[1]:x,
                        d[2]:y,
                        d[3]:z
                    }
                    end_dict.append(test)
    print(end_dict)
Asked By: willem12

||

Answers:

You can do it in a list comprehension that creates a dictionary entry with all keys for each combinations of p and a values:

d = ["a", "b", "c", "d"]
p = [1,2,3,4]
a = [1,2,3,4]

r = [ {dk:{"p":pv,"a":av} for dk in d} for pv in p for av in a ]

print(*r,sep="n")
{'a': {'p': 1, 'a': 1}, 'b': {'p': 1, 'a': 1}, 'c': {'p': 1, 'a': 1}, 'd': {'p': 1, 'a': 1}}
{'a': {'p': 1, 'a': 2}, 'b': {'p': 1, 'a': 2}, 'c': {'p': 1, 'a': 2}, 'd': {'p': 1, 'a': 2}}
{'a': {'p': 1, 'a': 3}, 'b': {'p': 1, 'a': 3}, 'c': {'p': 1, 'a': 3}, 'd': {'p': 1, 'a': 3}}
{'a': {'p': 1, 'a': 4}, 'b': {'p': 1, 'a': 4}, 'c': {'p': 1, 'a': 4}, 'd': {'p': 1, 'a': 4}}
{'a': {'p': 2, 'a': 1}, 'b': {'p': 2, 'a': 1}, 'c': {'p': 2, 'a': 1}, 'd': {'p': 2, 'a': 1}}
{'a': {'p': 2, 'a': 2}, 'b': {'p': 2, 'a': 2}, 'c': {'p': 2, 'a': 2}, 'd': {'p': 2, 'a': 2}}
{'a': {'p': 2, 'a': 3}, 'b': {'p': 2, 'a': 3}, 'c': {'p': 2, 'a': 3}, 'd': {'p': 2, 'a': 3}}
{'a': {'p': 2, 'a': 4}, 'b': {'p': 2, 'a': 4}, 'c': {'p': 2, 'a': 4}, 'd': {'p': 2, 'a': 4}}
{'a': {'p': 3, 'a': 1}, 'b': {'p': 3, 'a': 1}, 'c': {'p': 3, 'a': 1}, 'd': {'p': 3, 'a': 1}}
{'a': {'p': 3, 'a': 2}, 'b': {'p': 3, 'a': 2}, 'c': {'p': 3, 'a': 2}, 'd': {'p': 3, 'a': 2}}
{'a': {'p': 3, 'a': 3}, 'b': {'p': 3, 'a': 3}, 'c': {'p': 3, 'a': 3}, 'd': {'p': 3, 'a': 3}}
{'a': {'p': 3, 'a': 4}, 'b': {'p': 3, 'a': 4}, 'c': {'p': 3, 'a': 4}, 'd': {'p': 3, 'a': 4}}
{'a': {'p': 4, 'a': 1}, 'b': {'p': 4, 'a': 1}, 'c': {'p': 4, 'a': 1}, 'd': {'p': 4, 'a': 1}}
{'a': {'p': 4, 'a': 2}, 'b': {'p': 4, 'a': 2}, 'c': {'p': 4, 'a': 2}, 'd': {'p': 4, 'a': 2}}
{'a': {'p': 4, 'a': 3}, 'b': {'p': 4, 'a': 3}, 'c': {'p': 4, 'a': 3}, 'd': {'p': 4, 'a': 3}}
{'a': {'p': 4, 'a': 4}, 'b': {'p': 4, 'a': 4}, 'c': {'p': 4, 'a': 4}, 'd': {'p': 4, 'a': 4}}
Answered By: Alain T.