Slice a dictionary value from list of dictionaries in Python

Question:

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

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

Asked By: JamesDataAnalyst

||

Answers:

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'])
Answered By: Mime
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.