blindminds
blindminds

Reputation: 13

Updating Multi-level Nested Dictionary with a Multi-level Nested Dictionary

# Trying to Update a multi-dimensional dictionary using a multi-dimensional dictionary
# Iterate through existing dictionary with entries to create new multi-dimensional structured dictionaries
for entry_index, entry_data in entries.items():
        #print(f"Entry[{entry_index}]: {entry_data}")
        client_id = int(entry_data['CLIENT_ID'])
        matter_id = int(entry_data['MATTER_ID'])
        ref_num = entry_data['REF_NUM']
        tk_id = entry_data['TK_ID']
        date = entry_data['DATE'].strftime('%m-%d-%y')

        update_hierarchy = {
            client_id:{
                matter_id:{
                    ref_num:{
                        tk_id:{
                            date:{
                                entry_index:{
                                    'PHASE_TASK': entry_data['PHASE_TASK'],
                                    'ACTIVITY': entry_data['ACTIVITY'],
                                    'RATE': entry_data['RATE'],
                                    'HOURS': float(entry_data['HOURS']),
                                    'AMOUNT': entry_data['AMOUNT'],
                                    'DESCRIPTION': entry_data['DESCRIPTION'],
                                    'HOLD': entry_data['HOLD']
                                }
                            }
                        }
                    }
                }
            }
        }
        entries_hierarchy.update(update_hierarchy)
        # Expected behavior: Each nested key will be updated given nested dict structure...ie.
        # Expected Output:
        '''
        800:
            50:
                24:
                    12:
                        12.21.21:
                            0:
                                'Phase Task': Value,
                                'Activity': Value,
                                'Rate': Value,
                                'Hours': Value,
                                'Amount': Value,
                                'Description': Value,
                                'Hold': Value
                            1:
                                'Phase Task': Value,
                                'Activity': Value,
                                'Rate': Value,
                                'Hours': Value,
                                'Amount': Value,
                                'Description': Value,
                                'Hold': Value
            51:
                24:
                    12:
                        12.21.21:
                            2:
                                'Phase Task': Value,
                                'Activity': Value,
                                'Rate': Value,
                                'Hours': Value,
                                'Amount': Value,
                                'Description': Value,
                                'Hold': Value
                            3:
                                'Phase Task': Value,
                                'Activity': Value,
                                'Rate': Value,
                                'Hours': Value,
                                'Amount': Value,
                                'Description': Value,
                                'Hold': Value
        '''
        # Actual Result
        '''
        800:
            50:
                24:
                    12:
                        12.21.21:
                            0:
                                'Phase Task': Value,
                                'Activity': Value,
                                'Rate': Value,
                                'Hours': Value,
                                'Amount': Value,
                                'Description': Value,
                                'Hold': Value
                            1:
                                'Phase Task': Value,
                                'Activity': Value,
                                'Rate': Value,
                                'Hours': Value,
                                'Amount': Value,
                                'Description': Value,
                                'Hold': Value
        '''

        update_clients = {
            client_id: {
                entry_index: {
                    'MATTER_ID': matter_id,
                    'REF_NUM': ref_num,
                    'TK_ID': tk_id,
                    'DATE': date,
                    'PHASE_TASK': entry_data['PHASE_TASK'],
                    'ACTIVITY': entry_data['ACTIVITY'],
                    'RATE': entry_data['RATE'],
                    'HOURS': float(entry_data['HOURS']),
                    'AMOUNT': entry_data['AMOUNT'],
                    'DESCRIPTION': entry_data['DESCRIPTION'],
                    'HOLD': entry_data['HOLD']
                }
            }
        }
        entries_clients.update(update_clients)

The entries_clients.update(update_clients) ends up with only 1 entry per client id and expected behavior would be for update() to insert a new entry_id key under the client_id key if it does not exist

It seems Update() will only update the first key in a dict and ignores the nested keys...I am coming from PHP and finding working with arrays in Python not so intuitive.

Other questions I have found only talk about a single nested dctionary and not a multi-level one as such.

Upvotes: 1

Views: 400

Answers (1)

Daring_T
Daring_T

Reputation: 166

It looks like you've updated the wrong dict.

On the last line try changing it from:

entries_clients.update(update_clients)

to

entries_clients[client_id].update(update_clients)

Upvotes: 1

Related Questions