-2

I have three lists that I would like to make into keys for an empty triple nested dictionary using dictionary comprehension. "Facility" is the outer key, "Products" is the middle key, and "Customer" is the inner key. How can I do this? Thanks in advance

Here are my lists that need to be made into keys:

Facility = ['Fac-1', 'Fac-2','Fac-3','Fac-4','Fac-5']  
Products = ['45906','48402','80591','102795','107275','128067','129522',]  
Customer = ["Apple","Samsung","Huawei","Nokia","Motorolla"]
Phil
  • 31
  • 3
  • Please give a concrete example of the desired output. – John Gordon Aug 15 '21 at 17:47
  • There are no values, I need it to be empty because I will use this nested dictionary in an objective function in an optimization model (PuLP). The three lists I provided are the keys to the nested dictionaries, not the values. The model itself will assign values once I run it – Phil Aug 16 '21 at 17:38

1 Answers1

2

If by empty dict, you mean to have empty lists as values.

You could do that with a dict comprehension:

>>> d = {f: {p: {c: [] for c in Customer} for p in Products} for f in Facility}

Then you will get the data structure you described:

>>> d
{'Fac-1': {'102795': {'Apple': [], ...}, ...},
 'Fac-2': {'102795': {'Apple': [], ...}, ...},
 ...}

>>> d.keys()
dict_keys(['Fac-1', 'Fac-2', 'Fac-3', 'Fac-4', 'Fac-5'])

>>> d['Fac-1'].keys()
dict_keys(['45906', '48402', '80591', '102795', '107275', '128067', '129522'])

>>> d['Fac-1']['45906'].keys()
dict_keys(['Apple', 'Samsung', 'Huawei', 'Nokia', 'Motorolla'])
Ivan
  • 34,531
  • 8
  • 55
  • 100