1

My goal is to iterate through every element in classes and add the value of class in classes into a new list.

JSON Structure:

{
    "images": [
      {
        "classifiers": [
          {
            "classes": [
              {
                "class": "street",
                "score": 0.846
              },
              {
                "class": "road",
                "score": 0.85
              }
            ]
          }
        ]
      }
   ]
}

In the above JSON example, the new list should contain:

{'street','road'}

I tried to iterate over json_data['images'][0]['classifiers']['classes'] and for each one list.append() the value of class.

list = list()
def func(json_data):
    for item in json_data['images'][0]['classifiers']['classes']:
        list.append(item['class'])
    return(list)

I am receiving a TypeError which is:

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

What I am getting from this TypeError is that it attempts to add a string to the list but list.append() does not accept a string as a paramet. I need to somehow convert the string into something else.

My question is what should I convert the string into so that list.append() will accept it as a parameter?

16
  • 1
    Thanks for showing us your input & expected output. Now show us the code you've written to do this, and explain where you're stuck. Commented Jul 25, 2017 at 18:14
  • I keeep getting TypeError: list indices must be integers or slices, not str and using that error and trying things on StackOverflow to fix that error are not working. Commented Jul 25, 2017 at 18:16
  • 2
    Please include the full traceback as an edit too. Commented Jul 25, 2017 at 18:17
  • 3
    classifiers and classes are also lists. json_data['images'][0]['classifiers'][0]['classes']: Commented Jul 25, 2017 at 18:18
  • 1
    return set(lst) I think. If that is what you expect I'll have a go at writing an answer on my phone - always fun - so you can close this. Commented Jul 25, 2017 at 18:29

1 Answer 1

3

The first issue is that classifiers is also a list, so you need an additional index to get at classes. This will work:

for item in json_data['images'][0]['classifiers'][0]['classes']:

Second, you want the result as a set not a list, so you can do: return set(lst). Note that sets are unordered, so don't expect the ordering of items to match that of the list.

Sign up to request clarification or add additional context in comments.

7 Comments

How do I make it ordered?
Why do you need it to be both a set and ordered before we go down that route?
It actully does not need to be a set. It can be a list
I just need it to have the {}
What does this mean? Doing if ['images'][0]['classifiers'][0]["classes"] not in json_data: Gives me TypeError: string indices must be integers
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.