-2

Below is the dictionary framed in python.

risk = {'Main' : {'Cat1' : [['A', 'B', 'C', 'D'],['A1', 'B1', 'C1', 'D1']],'Cat2' : [['A2', 'B2', 'C2', 'D2'],['A3', 'B3', 'C3', 'D3']],'Cat3' : [['A4', 'B4', 'C4', 'D4'],['A5', 'B5', 'C5', 'D5']]}}  

Is there a way to split it like below

risk1

{'Main': {'Cat1': [['A', 'B', 'C', 'D'], ['A1', 'B1', 'C1', 'D1']]}}
risk2

{'Main': {'Cat2': [['A2', 'B2', 'C2', 'D2'], ['A3', 'B3', 'C3', 'D3']]}}
risk3

{'Main': {'Cat3': [['A4', 'B4', 'C4', 'D4'], ['A5', 'B5', 'C5', 'D5']]}}
Alen Paul Varghese
  • 1,278
  • 14
  • 27
navee pp
  • 77
  • 6

5 Answers5

0

No need to iterate over the dict.

risk = {
    'Main': {
        'Cat1': [['A', 'B', 'C', 'D'], ['A1', 'B1', 'C1', 'D1']],
        'Cat2': [['A2', 'B2', 'C2', 'D2'], ['A3', 'B3', 'C3', 'D3']],
        'Cat3': [['A4', 'B4', 'C4', 'D4'], ['A5', 'B5', 'C5', 'D5']]
    }
}

risk['Main']['Cat1'] accesses the value associated with the key 'Cat1' within the dictionary associated with the key 'Main' in the risk dictionary. You can create the new dicts as follow:

risk1 = {'Main': {'Cat1': risk['Main']['Cat1']}}
risk2 = {'Main': {'Cat2': risk['Main']['Cat2']}}
risk3 = {'Main': {'Cat3': risk['Main']['Cat3']}}


print("risk1\n", risk1)
print("risk2\n", risk2)
print("risk3\n", risk3)

OUTPUT

risk1
 {'Main': {'Cat1': [['A', 'B', 'C', 'D'], ['A1', 'B1', 'C1', 'D1']]}}

risk2
 {'Main': {'Cat2': [['A2', 'B2', 'C2', 'D2'], ['A3', 'B3', 'C3', 'D3']]}}

risk3
 {'Main': {'Cat3': [['A4', 'B4', 'C4', 'D4'], ['A5', 'B5', 'C5', 'D5']]}}
0

Consider a for comprehension such as

res = [ {'Main': {k: v}} for (k, v) in risk['Main'].items() ]

for extracting each entry in the dictionary, where each tuple (k, v) delivered from items() is converted into a dictionary key-value {k: v}. Hence

for xs in res:
    print(xs)

{'Main': {'Cat1': [['A', 'B', 'C', 'D'], ['A1', 'B1', 'C1', 'D1']]}}
{'Main': {'Cat2': [['A2', 'B2', 'C2', 'D2'], ['A3', 'B3', 'C3', 'D3']]}}
{'Main': {'Cat3': [['A4', 'B4', 'C4', 'D4'], ['A5', 'B5', 'C5', 'D5']]}}

iris
  • 379
  • 1
  • 2
  • 9
0
risk = {
    'Main': {
        'Cat1': [['A', 'B', 'C', 'D'], ['A1', 'B1', 'C1', 'D1']],
        'Cat2': [['A2', 'B2', 'C2', 'D2'], ['A3', 'B3', 'C3', 'D3']],
        'Cat3': [['A4', 'B4', 'C4', 'D4'], ['A5', 'B5', 'C5', 'D5']]
    }
}

split_risk = [{'Main': {category: data}} for category, data in risk['Main'].items()]

# Print the split risk dictionaries
for idx, split_dict in enumerate(split_risk, start=1):
    print(f'risk{idx}\n{split_dict}\n')
Shekhar
  • 1
  • 1
0

You should not be trying to create discrete variables. That's not an extensible solution. A better approach is to use a dictionary. Something like this:

risk = {'Main' : {'Cat1' : [['A', 'B', 'C', 'D'],['A1', 'B1', 'C1', 'D1']],'Cat2' : [['A2', 'B2', 'C2', 'D2'],['A3', 'B3', 'C3', 'D3']],'Cat3' : [['A4', 'B4', 'C4', 'D4'],['A5', 'B5', 'C5', 'D5']]}}

d = {}

for i, (k, v) in enumerate(risk['Main'].items(), 1):
    d[f'risk{i}'] = {'Main': {k: v}}

# then, for example...
print(d['risk3'])

Output:

{'Main': {'Cat3': [['A4', 'B4', 'C4', 'D4'], ['A5', 'B5', 'C5', 'D5']]}}
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

You don't need the variable names to be risk1, risk2 and risk3. It's not a scalable solution. Read this SO post for details. I'd use @DarkKnight's solution, but if you really want the variables to be named as above, you can use tuple unpacking to get the variables as desired.

risk = {'Main': {'Cat1': [['A', 'B', 'C', 'D'], ['A1', 'B1', 'C1', 'D1']],
                 'Cat2': [['A2', 'B2', 'C2', 'D2'], ['A3', 'B3', 'C3', 'D3']],
                 'Cat3': [['A4', 'B4', 'C4', 'D4'], ['A5', 'B5', 'C5', 'D5']]}}

risks = []

for k, v in risk['Main'].items():
    risk_tmp = {'Main': {k: v}}
    risks.append(risk_tmp)

risk1, risk2, risk3 = tuple(risks)

print(risk1)
print(risk2)
print(risk3)

To get the variable names as risk{n}, you can use a separate script to generate the variable names, copy and paste it into code. Again, I really don't recommend this, but here is a sample script if you really need to.

p = [f"risk{i}" for i in range(1, 11)]
for i in p:
    print(i, end=", ")
print()
berinaniesh
  • 198
  • 13