简体   繁体   中英

how to format list of dictionaries to nested dictionary in python

I am fetching huge data as list of dictionary. But, I would like to reformat as nested dictionaries. I am not sure how nested dictionary work. I would like add the code I have tried but seems I am getting error while posting. Can't able to add more codes. I will add in comment section. My list of dictionaries looks in this.

    source = [
  {
    "account_id": "111111111111",
    "instance_id": "i-xxxxxxxxxxx",
    "instance_profile_arn": "arn:aws:iam::111111111111:instance-profile/x",
    "region_name": "eu-central-1"
  },
  {
    "account_id": "111111111111",
    "instance_id": "i-aaaaaaaaaaa",
    "instance_profile_arn": "arn:aws:iam::111111111111:instance-profile/aa",
    "region_name": "us-east-1"
  },
  {
    "account_id": "22222222222",
    "instance_id": "i-bbbbbbbbb",
    "instance_profile_arn": "arn:aws:iam::22222222222:instance-profile/sadf",
    "region_name": "eu-central-1"
  },
  {
    "account_id": "22222222222",
    "instance_id": "i-ccccccccccc",
    "instance_profile_arn": "arn:aws:iam::22222222222:instance-profile/sds",
    "region_name": "us-east-1"
  },
  {
    "account_id": "33333333333",
    "instance_id": "i-eeeeeeeee",
    "instance_profile_arn": "arn:aws:iam::33333333333:instance-profile/dsf",
    "region_name": "eu-west-1"
  }

I would like to format like this.

    {
    "111111111111": {
        "eu-central-1": {
            "i-xxxxxxxxxxx": "arn:aws:iam::111111111111:instance-profile/x"
        },
        "us-east-1": {
            "i-aaaaaaaaaaa": "arn:aws:iam::111111111111:instance-profile/aa"
        }
    },
    "22222222222": {
        "eu-central-1": {
            "i-bbbbbbbbb": "arn:aws:iam::22222222222:instance-profile/sds"
        },
        "us-east-1": {
            "i-ccccccccccc": "arn:aws:iam::22222222222:instance-profile/sds"
        }
    },
    "33333333333": {
        "eu-west-1": {
            "i-eeeeeeeee": "arn:aws:iam::33333333333:instance-profile/dsf"
        }
    }
}

The code I have tried.

for each in source:
    list_dict.append({
        each['account_id']: {
            each['region_name']: {
                each['instance_id']: each['instance_profile_arn']
            }
        }
    })

print(list_dict)

I would like to write the output to csv file. I would appreciate any help.

try this, usingsetdefault to init missing keys & update the values on go.

source = { .. } # source dict

result = {}

for v in source:
    (result.setdefault(v['account_id'], {})
        .setdefault(v['region_name'], {})
        .update({v['instance_id']: v['instance_profile_arn']}))

print(result)

{'111111111111': {'eu-central-1': {'i-xxxxxxxxxxx': 'arn:aws:iam::111111111111:instance-profile/x'},
                  'us-east-1': {'i-aaaaaaaaaaa': 'arn:aws:iam::111111111111:instance-profile/aa'}},
 '22222222222': {'eu-central-1': {'i-bbbbbbbbb': 'arn:aws:iam::22222222222:instance-profile/sadf'},
                 'us-east-1': {'i-ccccccccccc': 'arn:aws:iam::22222222222:instance-profile/sds'}},
 '33333333333': {'eu-west-1': {'i-eeeeeeeee': 'arn:aws:iam::33333333333:instance-profile/dsf'}}}

You could do something like:

 result = dict();
 for item in source:
     account: = item['account_id']
     region = item['region_name']
     
     if account not in result:
         result[account] = dict()
     
     if region not in result[account]:
         result[account][region] = dict()
     
     instance = item['instance_id']
     instance_profile = item['instance_profile_arn']
     result[account][region][instance] = instance_profile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM