1

I have looked through few questions regarding nested dictionary such as link_a, link_b, and link_c. But i cant seem to find the solution (or i couldnt figure it out). So i already have a dictionary which is in this format:{'key_01': ['value1', 'value2'], key_02: ....} which goes until key_08 where certain keys have different amount of 'value' in them.

For each value1 and value2 has 2 more additional values which are x and y in which i will obtain once i run my program.

So the final output that i want it as below:

{'key_01': 
   ['value1' : 
     ['x': 2], 
     ['y': 3]], 
   ['value2': 
     ['x': 2], 
     ['y': 3]], ....}

This is one of the method i tried by using this tutorial for adding into dictionary:

x = 5
for i in range(len(demo_dict['key_01'])):
    for val in demo_dict['key_01']:
        demo_dict['key_01'][0] = x

But it changed the value1 to 5 which is not what i want. And i know i cannot use append. Now im stuck.

2 Answers 2

1

I believe you need

data = {'key_01': ['value1', 'value2'], key_02: ....} 
result = {}
for k, v in data.items():
    for i in v:
        result.setdefault(k, {}).update({'v': {'x': x, 'y': y}}) 

Output:

{'key_01': {
    'v': {
        'x': x, 
        'y': y
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

every pair of 'key':value should be in a dictionary, like:

dict = {'key_01':[
          {'value1':{'x': 2,'y': 3}, 
           'value2':{'x': 2,'y': 3}, 
           'value3': ....]}

So key_01 has a list of dictionaries value1,value2,value3, where each has a dictionary for x and y.

this way you can access the 'x' and 'y' values as

dict['value_01']['value1']['x'] and dict['value_01']['value1']['x']

1 Comment

Ive made a little research regarding dict in a dict and seems like i got the format that i want. but it seems to not append the second value (value2). i'll share my code in the update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.