Question: I am trying to update an array of Labels which are on a form from a backgroundworker. Here is my code:

```
for (int i = 0; i < 6; i++)
{
    if (this.InvokeRequired)
    {
        this.Invoke((MethodInvoker)delegate
        {
            arrLabel[i].Text = values[i].ToString();
        });
    }
    else
    {
        arrLabel[i].Text = values[i].ToString();
    }
}

```

This does not work, but if I try to change text property of each label instead of the array, it works. How can I fix this? Also is there a shorter/better way of updating form controls from backgroundworkers than what I am doing for every single control on my form?

Edit: here is how I defined the array:

```
  private Label[] arrLabel = new Label[6];

```

and here is the function that I call to assign the array:

```
    private void makeLabelArrays()
    {
        for (int i = 0; i < 6; i++)
        {
            arrLabel[i] = (Label)Groupbox1.Controls["label" + (i + 1).ToString()];
        }
    }

```

Answer: 
You can do the following:

1. Transform a list of `Classifications` to a nested dict.
2. Transform nested dict to the required format

Samples here will operate on slightly reduced `Classification` class to improve readability:

```
class Classification:
    def __init__(self, kingdom, phylum, klass, species):
        self.kingdom = kingdom
        self.phylum = phylum
        self.klass = klass
        self.species = species

```

First part:

```
from collections import defaultdict
# in order to work with your actual implementation add more levels of nesting 
# as lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
nested_dict = defaultdict(
    lambda: defaultdict(
        lambda: defaultdict(list)
    )
)

for c in all_classifications:
    nested_dict[c.kingdom][c.phylum][c.klass].append(c.species)

```

[`defaultdict`](https://docs.python.org/2/library/collections.html#collections.defaultdict) is just a nice tool to guarantee existence of the key in a dictionary, it receives any callable and use it to create a value for missing key.

Now we have nice nested dictionary in the form of 

```
{
    'Kingdom1': { 
        'Phylum1': { 
            'Class1': ["Species1", "Species2"],
            'Class2': ["Species3", "Species4"],
        },
        'Phylum2': { ... }
     },
     'Kingdom2': { 'Phylum3': { ... }, 'Phylum4': {... } }
}

```

Part two: converting to desired output

```
def nested_to_tree(key, source):
    result = {'name': key, 'children':[]}
    for key, value in source.items():
        if isinstance(value, list):
            result['children'] = value
        else:
            child = nested_to_tree(key, value)
            result['children'].append(child)

    return result

tree = nested_to_tree('root', nested_dict')

```

I believe it's self-explanatory - we just convert passed dictionary to desired format and recurse to it's content to form children.

Complete example is [here](http://pastebin.com/xHHaLcqx).

Two notes: 

1. Written in python 3. Replacing `source.items()` with `source.iteritems()` should suffice to run in python 2.
2. You haven't specify what leafs should looks like, so I just assumed that leaf nodes should be `genus` with all `species` attached as `children`. If you want `species` to be leaf nodes - it's pretty straightforward to modify the code to do so. If you have any trouble doing so - let me know in comments.
Finally got what I wanted. Code is not beautiful, near ugly, yet somehow I got what I wanted.

```
def classification_flare_json(request):
    #Extracting from database and sorting the taxonomy from left to right
    clazz = Classification.objects.all().order_by('kingdom','phylum','class_field','genus','species')

    tree = {'name': "root", 'children': []}

    #To receive previous value of given taxa type
    def get_previous(type):
        types = ['kingdom', 'phylum', 'class_field', 'family', 'genus', 'species']
        n = types.index(type)

        sub_tree = tree['children']
        if not sub_tree: return None
        for i in range(n):
            if not sub_tree: return None
            sub_tree = sub_tree[len(sub_tree)-1]['children']

        if not sub_tree: return None
        last_item = sub_tree[len(sub_tree)-1]
        return last_item['name']

    #To add new nodes in the tree
    def append(type, item):
        types = ['kingdom', 'phylum', 'class_field', 'family', 'genus', 'species_id']
        n = types.index(type)

        sub_tree = tree['children']
        for i in range(n+1):
            if not sub_tree: return None
            sub_tree = sub_tree[len(sub_tree)-1]['children']

        sub_tree.append(item)

    for item in clazz:
        while True:
            if item.kingdom == get_previous('kingdom'):
                if item.phylum == get_previous('phylum'):
                    if item.class_field == get_previous('class_field'):
                        if item.family == get_previous('family'):
                            if item.genus == get_previous('genus'):
                                append('genus', {'name':item.species, 'size': 1})
                                break;
                            else:
                                append('family', {'name':item.genus, 'children': []})
                        else:
                            append('class_field', {'name':item.family, 'children':[]})
                    else:
                        append('phylum', {'name': item.class_field, 'children':[]})
                else:
                    append('kingdom', {'name': item.phylum, 'children':[]})
            else:
                tree['children'].append({'name': item.kingdom, 'children':[]})

    return HttpResponse(json.dumps(tree), content_type="application/json")

```