StackUser
StackUser

Reputation: 475

Retrieve dictionary which satisfies some condition

I have a dictionary as shown below:

x = {
   "Student": [
       {
        "tags": {
                    "name": "Alex",
                    "class": "Biology",
                    "gender": "male",
                         },
        "Nationality": "",
        "Test Score": 10.0,
        "Exam Score": 70.0,
                
        },
{
        "tags": {
                    "id": "A123",
                    "height": "170",
                    "age": "15",
                         },
        "Nationality": "",
        "Test Score": 20.0,
        "Exam Score": 80.0,
                
        },
              ],
    }

I would like to obtain the Test score and exam score for the data schema above for which has a nested dictionary with key 'tag' has a the key of "id","height" and "age"

So the expected value should be "Test Score"=20.0 and "Exam Score"=80.0

I have tried the below implementation but it seems to check for only the first value in the 'Student' list (of lenght 2), but i need it to check for all the items in the list (in this case two items).

search_tag = ["id", "height", "age"]
    val_list = []

    if all([t in search_tag for t in x["Student"][0]["tags"].keys()]):
        val_list.append(x["Student"][0]["Test Score"])
        val_list.append(x["Student"][0]["Exam Score"]) 

Upvotes: 2

Views: 53

Answers (3)

Mark
Mark

Reputation: 1983

x = {'Student': [{'Exam Score': 70.0,
              'Nationality': '',
              'Test Score': 10.0,
              'tags': {'class': 'Biology', 'gender': 'male', 'name': 'Alex'}},
             {'Exam Score': 80.0,
              'Nationality': '',
              'Test Score': 20.0,
              'tags': {'age': '15', 'height': '170', 'id': 'A123'}}]}

search_tag = {"age", "height", "id"}
for student in x["Student"]:
    if search_tag.issubset(student["tags"]):
        print([student["Test Score"], student["Exam Score"]])

Use a set to check for the criteria.

This prints

[20.0, 80.0]

Upvotes: 1

Andrej Kesely
Andrej Kesely

Reputation: 195528

You can compare set of your keys to .keys():

x = {
    "Student": [
        {
            "tags": {
                "name": "Alex",
                "class": "Biology",
                "gender": "male",
            },
            "Nationality": "",
            "Test Score": 10.0,
            "Exam Score": 70.0,
        },
        {
            "tags": {
                "id": "A123",
                "height": "170",
                "age": "15",
            },
            "Nationality": "",
            "Test Score": 20.0,
            "Exam Score": 80.0,
        },
    ],
}

to_search = {"age", "id", "height"}

for student in x["Student"]:
    if student["tags"].keys() == to_search:
        print(student["Test Score"])
        print(student["Exam Score"])

Prints:

20.0
80.0

Upvotes: 5

quamrana
quamrana

Reputation: 39404

You just need to iterate over the list indexed by "Student":

x = {
   "Student": [
       {
        "tags": {
                    "name": "Alex",
                    "class": "Biology",
                    "gender": "male",
                         },
        "Nationality": "",
        "Test Score": 10.0,
        "Exam Score": 70.0,
                
        },
       {
        "tags": {
                    "id": "A123",
                    "height": "170",
                    "age": "15",
                         },
        "Nationality": "",
        "Test Score": 20.0,
        "Exam Score": 80.0,
                
        },
    ],
  }

search_tag = ["id", "height", "age"]

val_list = []
for item in x["Student"]:
    if all(t in search_tag for t in item['tags']):
        val_list.append(item["Test Score"])
        val_list.append(item["Exam Score"])
print(val_list)

Output:

[20.0, 80.0]

Upvotes: 2

Related Questions