0

I have below dictionary and I need to extract "red" and "blue" values as list, there may be a lot more values than only these 2.

input:

js = {
    "accounts": {
        "red": {
            "client_id": "123",
            "client_secret": "123",
        },
        "blue": {
            "client_id": "123",
            "client_secret": "123",
        }
    }
}

expected output:

["red","blue"]
a ssssss
  • 13
  • 1

1 Answers1

1

It's actually pretty simple. There's no need to use dict.keys() at all...

js = {
    "accounts": {
        "red": {
            "client_id": "123",
            "client_secret": "123",
        },
        "blue": {
            "client_id": "123",
            "client_secret": "123",
        },
        "green": {
            "client_id": "123",
            "client_secret": "123",
        }
    }
}

print (list(js["accounts"]))

Output:

['red', 'blue', 'green']
Justin
  • 1,006
  • 12
  • 25