Python – Using dictionary understanding Python removes multiple key items

Using dictionary understanding Python removes multiple key items… here is a solution to the problem.

Using dictionary understanding Python removes multiple key items

I

want to remove multiple keys from my json, which I am using to understand using such a dictionary

remove_key = ['name', 'description']

data = {'full-workflow': {'task_defaults': {'retry': {'count': 3, 'delay': 2}}, 'tasks': {'t1': {'action': 'nn.postgres.export-db', 'description': 'Dump prod DB with default settings', ' input': {'database': 'prod', 'filepath': '/var/tmp/prod-dump.pgsql', 'host': 'postgres.local', 'password': 'mypass', 'username': 'myuser'}, 'name': 'db export', 'on-success': ['t2'] }, 't2': {'action': 'nn.aws.upload-to-s3', 'description': 'Upload to S3 bucket for development', 'input': {'sourcepath': '{{ tasks(t1).result.filepath }}', 'targetpath': 's3:// mybucket/prod-dump.pgsql'}, 'name': 'Upload to S3', 'on-success': ['t3'], 'retry': {'count': 5, 'delay': 5}}, 't3': {'action': 'nn.shell.command', 'description': 'Remove temp file from batch folder ', 'input': {'cmd': 'rm {{ tasks(t1).result.filepath }}'}, 'name': 'Remove temp file', 'on-complete': ['t4']}, 't4': {'action': 'nn.notify.send-mail', 'description': 'Send email to admin containing target path', 'input': {'from': '[email protected]', 'message': 'DB Dump {{ tasks(t1).result.filepath }} was stored to S3', 'subject': 'Prod DB Backup', 'to': '[email protected]' }, 'name': 'Send email', 'target': 'nn'}}}, 'version': '2'}

def remove_additional_key(data):
        return {
            key: data[key] for key in data if key not in remove_key
        }

And then

new_data = remove_additional_key(data)

Because this is a nested dictionary and I want to remove_key from the tasks dictionary, what am I doing wrong?

Solution

Your data is a nested dictionary. If you want to remove any data for the keys contained in remove_key, then I recommend using the recursive approach. This can be easily achieved depending on your exit function remove_additional_key:

def remove_additional_key(data):

sub_data = {} 
    for key in data:
        if key not in remove_key:
            if isinstance(data[key], dict): # if is dictionary
                sub_data[key] = remove_additional_key(data[key])
            else:
                sub_data[key] = data[key]
    return sub_data

new_data = remove_additional_key(data)

Note that if an entry is a dictionary, it can be checked by isinstance(data[key], dict). See also How to check if a variable is a dictionary in Python?

Related Problems and Solutions