Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Slice a dictionary value from list of dictionaries in Python

I am trying to append a specific value from my List of Dictionary to another list.

All_Result={'Team Name':[]}

Trying to append

all_result['SupportGroup'].append(issue["fields"]["Team Name"]['objectId'])

and this gives me

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: list indices must be integers or slices, not str

How to get the objectId from the below list of dict to append it to All_Result?

This is my List of Dictionary

"Team Name" = [{
    'workspaceId': 'fdxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'id': 'fdxxxxxxxxxxxxxxxxxx',
    'objectId': '1234'
}]

Kindly help

>Solution :

It looks like the value for the key ‘Team Name’ in your issue["fields"] dictionary is not a dictionary itself, but rather a list of dictionaries. Therefore, you need to use an index to access the dictionary within the list that contains the ‘objectId’ key.

Assuming issue["fields"]["Team Name"] is the list of dictionaries, you can access the first dictionary in the list (which should be the only one based on your example) and then get the ‘objectId’ value using the key. Here’s an example of how you could modify your code to append the ‘objectId’ value to All_Result[‘Team Name’]:

All_Result = {'Team Name': []}
# Assuming issue["fields"]["Team Name"] is a list of dictionaries
team_dict = issue["fields"]["Team Name"][0]
All_Result['Team Name'].append(team_dict['objectId'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading