'Get specific items in python dict - listaccounts (AWS)

I get the response as is printed below, I needed to get from accounts only id, email and name. Also NextToken.

{
    'Accounts': [
        {
            'Id': '19',
            'Arn': 'arn:aws:organizations: : 00000000:account/o-00000000nmnm',
            'Email': '[email protected]',
            'Name': 'name',
            'Status': 'ACTIVE',
            'JoinedMethod': 'CREATED',
            'JoinedTimestamp': datetime.datetime(2020,3,12,11,49,53,790000, tzinfo=tzlocal())
        },
        {
            'Id': '193232',
            'Arn': 'arn:aws:organizations: : 00000000:account/relrkeler528xeftox/3643851343443434385620',
            'Email': '[email protected]',
            'Name': 'anothername',
            'Status': 'ACTIVE',
            'JoinedMethod': 'CREATED',
            'JoinedTimestamp': datetime.datetime(2021,7,26,20,24,34,568000, tzinfo=tzlocal())
        },
        {
            'Id': '576',
            'Arn': 'arn:aws:organizations: : 156455455445645640:account/we528xeftox/57682104',
            'Email': '[email protected]',
            'Name': 'nameok',
            'Status': 'ACTIVE',
            'JoinedMethod': 'CREATED',
            'JoinedTimestamp': datetime.datetime(2020,3,10,19,29,44,257000, tzinfo=tzlocal())
        }
    ],
    'NextToken': 'HJhjkHUIhUIHuhJUIHuhUIHuihJjkhJKHjkhJKHjkHJ',
    'ResponseMetadata': {
        'RequestId': 'd4FKDJDFK-484785-FDKDFJFDKFJDF',
        'HTTPStatusCode': 200,
        'HTTPHeaders': {
            'x-amzn-requestid': 'ERUIERUOERUI-2121-jhsjhsakjhasjk',
            'content-type': 'application/x-amz-json-1.1',
            'content-length': '1556',
            'date': 'Thu, 31 Mar 2022 17: 47: 00 GMT'
        },
        'RetryAttempts': 0
    }
}

I'm new and have searched how to iterate through items in a key, tried the item inside key but haven't found the right way.

I appreciate the help.



Solution 1:[1]

Assuming your dictionary is named data

required_data = {'Accounts': []}
for item in data['Accounts']:
    required_data['Accounts'].append({
        'Id': item['Id'],
        'Name': item['Name'],
        'Email': item['Email']
    })
required_data['NextToken'] = data['NextToken']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sheikh Abdul Manan