0

I have this list where I want to store a name, and inside the name more itemlists with a name and amount, well you can see the rough outline below.

varx = [
    {"Name1":
        {
        "itemList":
            {
            "item1":100,
            "item2":100,
            "item3":100,
            "item4":100,
            "item5":100
            },
        "otherStuff":
            {
            "stuff1":100,
            "stuff2":100,
            "stuff3":100,
            }
        },
    "Name2":
        {
        "itemList":
            {
            "item1":100,
            "item2":100,
            "item3":100,
            "item4":100,
            "item5":100
            },
        "otherStuff":
            {
            "stuff1":100,
            "stuff2":100,
            "stuff3":100
            }}}]

Assume there's "name1" through "name50", how would I add an item inside the itemlist of "name34" if all you knew were the "name34" and not it's index?

user2835948
  • 193
  • 1
  • 2
  • 8
Kevin Ferm
  • 113
  • 3
  • 9
  • 1
    Do you need it to be a list? This is a list with 1 element. – Hoopdady Oct 31 '13 at 18:10
  • http://stackoverflow.com/questions/651794/whats-the-best-way-to-initialize-a-dict-of-dicts-in-python Check that out. It's what you are looking for I think. – virtuexru Oct 31 '13 at 18:14

1 Answers1

0

Just use a nested dictionary (ie, loose the list part):

data={"Name34":
        {
        "itemList":
            {
            "item1":100,
            "item2":100,
            "item3":100,
            "item4":100,
            "item5":100
            },
        "otherStuff":
            {
            "stuff1":100,
            "stuff2":100,
            "stuff3":100,
            }
        },
    "Name2":
        {
        "itemList":{'items'
        }}}

data['Name34']["itemList"]['item6']=100   

print(data['Name34']["itemList"])

Prints:

{'item1': 100, 'item3': 100, 'item2': 100, 'item5': 100, 'item4': 100, 'item6': 100}

If you want autovivication of the intermediate levels, you can do something along these lines:

from collections import defaultdict
data = defaultdict(lambda: defaultdict(dict))

data['Name34']['IntemList']['Item1']=10
data['Name34']['IntemList']['Item6']=100

print(data['Name34']['IntemList'])

Prints:

{'Item1': 10, 'Item6': 100}

Which you can turn directly into json:

from collections import defaultdict
import json

data = defaultdict(lambda: defaultdict(dict)) 

i=0
for k1 in ['Name{}'.format(j) for j in range(1,6)]:
    for k2 in ('Item1', 'Item2', 'Item33'):
        data[k1]['ItemList'][k2]=i
        i+=1

print(json.dumps(data)) 

Prints:

{"Name5": {"ItemList": {"Item33": 14, "Item2": 13, "Item1": 12}}, 
 "Name4": {"ItemList": {"Item33": 11, "Item2": 10, "Item1": 9}}, 
 "Name3": {"ItemList": {"Item33": 8, "Item2": 7, "Item1": 6}}, 
 "Name2": {"ItemList": {"Item33": 5, "Item2": 4, "Item1": 3}}, 
 "Name1": {"ItemList": {"Item33": 2, "Item2": 1, "Item1": 0}}}
dawg
  • 80,841
  • 17
  • 117
  • 187