Peter Farmer
Peter Farmer

Reputation: 9370

Is there an elegant way to create a dictionary of arrays?

I frequently analyse data which has a parent child relationship:

data = [
    {'data': 'somedata', 'id': 1, 'parentId': 0},
    {'data': 'somedata', 'id': 2, 'parentId': 1},
    {'data': 'somedata', 'id': 3, 'parentId': 0},
    {'data': 'somedata', 'id': 4, 'parentId': 3},
    {'data': 'somedata', 'id': 5, 'parentId': 3},
]

Typically I'll use a loop like this to create a new data structure so I can easily relate parent and child data:

for item in data:
    if item["parentId"] != 0:
        if item["parentId"] in parents:
            parents[item["parentId"]].append(item["id"])
        else:
            parents[item["parentId"]] = []
            parents[item["parentId"]].append(item["id"])

This produces the following data:

print parents
{1: [2], 3: [4, 5]}

Is there a more elegant way to create the "parents" data structure?

Upvotes: 0

Views: 399

Answers (2)

gruszczy
gruszczy

Reputation: 42188

I don't know, what do you mean by more elegant. If you are writing some parsing script, than it seems that using built in structures is ok. So are you asking about used data structures or your code?

One thing I see is that you can use setdefault instead of checking if you have certain key in dictionary:

for item in data:
    if item["parentId"] != 0:
        parents.setdefault(item["parentId"], []).append(item['id'])

Upvotes: 4

ncoghlan
ncoghlan

Reputation: 41496

If your Python version includes collections.defaultdict you can do:

from collections import defaultdict

def make_parent_dict(data):
    parents = defaultdict(list) # Use empty list for missing entries
    for item in data:
        parent = item['parentId']
        if parent != 0:
            parents[parent].append(item['id'])
    return dict(parents) # Convert back to normal dict

example = [
    {'data': 'somedata', 'id': 1, 'parentId': 0},
    {'data': 'somedata', 'id': 2, 'parentId': 1},
    {'data': 'somedata', 'id': 3, 'parentId': 0},
    {'data': 'somedata', 'id': 4, 'parentId': 3},
    {'data': 'somedata', 'id': 5, 'parentId': 3},
]

>>> print make_parent_dict(example)
{1: [2], 3: [4, 5]}

Upvotes: 2

Related Questions