-1

I'm receiving JSON back from an API call want to log when a keyword has been detected, sometimes there may be one, none or several returned from the API. I'm able to log the data that comes back no problem.

I want to run 1000s of requests and then have each result logged as a list of results within a list, (So I know which list corresponds to which API call).

for item in response['output']['keywords']:
    TempEntityList = []
    TempEntityList.append(item['keywords'])
    EntityList.extend(TempEntityList)
    TempEntityList = []

Which does append everything to a list but I can't seem to find the right setup.

I get the below when I run it twice I get.

['Chat', 'Case', 'Telephone','Chat', 'Case', 'Telephone']

When really I want

[['Chat', 'Case', 'Telephone'],['Chat', 'Case', 'Telephone']]

I'm creating TempEntityList appending any matches found to it, extending EntityList by what has been found and then clearing TempEntityList down for the next API call.

What's the best way I could log each set of results to a nested list as so far I've only managed to get one large list or every item is it's own nested item.

As requested the payload that is returned looks like the below

{
  "output": {
    "intents": [],
    "entities": [
      {
        "entity": "Chat",
        "location": [
          0,
          4
        ],
        "value": "Chat",
        "confidence": 1
      },
      {
        "entity": "Case",
        "location": [
          5,
          9
        ],
        "value": "Case",
        "confidence": 1
      },
      {
        "entity": "Telephone",
        "location": [
          10,
          19
        ],
        "value": "Telephony",
        "confidence": 1
      }
    ],
    "generic": []
  },
  "context": {
    "global": {
      "system": {
        "turn_count": 1
      },
      "session_id": "xxx-xxx-xxx"
    },
    "skills": {
      "main skill": {
        "user_defined": {
          "Case": "Case",
          "Chat": "Chat",
          "Telephone": "Telephony"
        },
        "system": {
          "state": "x"
        }
      }
    }
  }
}
{
  "output": {
    "intents": [],
    "entities": [
      {
        "entity": "Chat",
        "location": [
          0,
          4
        ],
        "value": "Chat",
        "confidence": 1
      },
      {
        "entity": "Case",
        "location": [
          5,
          9
        ],
        "value": "Case",
        "confidence": 1
      },
      {
        "entity": "Telephone",
        "location": [
          10,
          19
        ],
        "value": "Telephony",
        "confidence": 1
      }
    ],
    "generic": []
  },
  "context": {
    "global": {
      "system": {
        "turn_count": 1
      },
      "session_id": "xxx-xxx-xxx"
    },
    "skills": {
      "main skill": {
        "user_defined": {
          "Case": "Case",
          "Chat": "Chat",
          "Telephone": "Telephony"
        },
        "system": {
          "state": "xxx-xxx-xxx"
        }
      }
    }
  }
}
{
  "output": {
    "intents": [],
    "entities": [
      {
        "entity": "Chat",
        "location": [
          0,
          4
        ],
        "value": "Chat",
        "confidence": 1
      },
      {
        "entity": "Case",
        "location": [
          5,
          9
        ],
        "value": "Case",
        "confidence": 1
      },
      {
        "entity": "Telephone",
        "location": [
          10,
          19
        ],
        "value": "Telephony",
        "confidence": 1
      }
    ],
    "generic": []
  },
1

2 Answers 2

1

Firstly, since you have TempEntityList = [] in the beginning of the for loop, you don't need to add another TempEntityList = [] in the bottom. To answer the question, use list.append() instead of list.extend():

for item in response['output']['keywords']:
    TempEntityList = []
    TempEntityList.append(item['keywords'])
    EntityList.append(TempEntityList)
Sign up to request clarification or add additional context in comments.

5 Comments

I tried with append too that gives me each item separately [['Chat'],['Case'],['Telephone']] as I don't know how many (if any) keywords the API is going to return I would just end up with a long list and I wouldn't able to match them to each individual call. You're right about the TempEntityList though that was an obvious miss thanks!
can you give an example response?
@CallumK showing what the response looks like would help you get a better answer here.
I've added the response back from the endpoint now thanks
why not EntityList = [item['keywords']] ?
-1

I've managed to get what I want, thanks everyone for the suggestions.

The solution was:

global EntityList
EntityList = []
for item in response['output']['entities']:
    EntityList.append(item['entity'])
    FinalList.append(EntityList)

Which after running the function for three times on the same input produced:

[['Chat', 'Case', 'Telephone'], ['Chat', 'Case', 'Telephone'], ['Chat', 'Case', 'Telephone']]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.