I
I
ItzGuyMc2022-01-21 22:07:29
Python
ItzGuyMc, 2022-01-21 22:07:29

How to add a class call to the list?

With the help of a get request, I get data of the form:

{
  "status": true,
  "data": [
    {
      "id": 1467,
      "name": "name1",
    },
    {
      "id": 3506,
      "name": "name1",
    },
    {
      "id": 7135,
      "name": "name2",
    },
    {
      "id": 996,
      "name": "name2",
    }
  ]
}

I would like the code to run through the data, leave only those lines where name: name1 (I’ll talk about this later) and then add class calls to the opt list:
disnake.SelectOption(
                label="id", description="name1", emoji=""
            )

I tried to filter the data - it did not work, I tried using
out = [x for x in data_dict if x['data']['name'] == 'name1']
, got an error TypeError: string indices must be integers, but this is optional.
I tried to add data to the list using append, but I can’t do it for all data in JSON, I tried to describe it as clearly as possible, if something is not clear - ask :-)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-01-21
@ItzGuyMc

x for x in
data_dict

[item for item in data_dict['data'] if item['name'] == 'name1']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question