Access values from multiple dictionaries within a list

Question:

I was making a scraper for my school website, which automatically gets the homework for today.

[
    {
        "id": "1523958",
        "studentId": "8326",
        "assignmentId": "35074",
        "diaryType": "cw",
        "diaryId": "35074",
        "subject": "URDU",
        "title": "اچھے آداب و اطوار",
        "description": "9F Maymaar - Classwork posted",
        "bRead": "1",
        "date": "Fri, 04/11/2022",
    },
    {
        "id": "1520938",
        "studentId": "8426",
        "assignmentId": "35013",
        "diaryType": "cw",
        "diaryId": "35013",
        "subject": "SOCIAL STUDIES",
        "title": "The Globe and Maps ",
        "description": "5C Maymaar - Classwork posted",
        "bRead": "0",
        "date": "Fri, 04/11/2022",
        "download": "2bf0c7b51af0cfb1023261dd7b8b4f8f.jpg",
    },
    {
        "id": "1520624",
        "studentId": "8426",
        "assignmentId": "35007",
        "diaryType": "cw",
        "diaryId": "35007",
        "subject": "MATHEMATICS",
        "title": "Percentage ",
        "description": "5C Maymaar - Classwork posted",
        "bRead": "0",
        "date": "Fri, 04/11/2022",
    },
    {
        "id": "1520530",
        "studentId": "8426",
        "assignmentId": "35005",
        "diaryType": "cw",
        "diaryId": "35005",
        "subject": "ENGLISH A",
        "title": "Paragraph writing ",
        "description": "5C Maymaar - Classwork posted",
        "bRead": "0",
        "date": "Fri, 04/11/2022",
    },
    {
        "id": "1517928",
        "studentId": "8326",
        "assignmentId": "34952",
        "diaryType": "cw",
        "diaryId": "34952",
        "subject": "PHYSICS",
        "title": "Homework",
        "description": "9F Maymaar - Classwork posted",
        "bRead": "1",
        "date": "Fri, 04/11/2022",
    },
    {
        "id": "1513747",
        "studentId": "8426",
        "assignmentId": "34887",
        "diaryType": "gn",
        "diaryId": "34887",
        "subject": None,
        "title": "General notice ",
        "description": "5C Maymaar - Notice posted",
        "bRead": "0",
        "date": "Thu, 03/11/2022",
    },
    {
        "id": "1508998",
        "studentId": "8426",
        "assignmentId": "34787",
        "diaryType": "cw",
        "diaryId": "34787",
        "subject": "SOCIAL STUDIES",
        "title": "Map reading skills ",
        "description": "5C Maymaar - Classwork posted",
        "bRead": "0",
        "date": "Thu, 03/11/2022",
    },
]

what you see above is a list, what i want to do is:
if studentId = 8426 and if date = something:
SEND AssignmentiId TO DISPLAY

i tried to do it my self but tbh i dont know how to approach this problem, how do i read every dictionary, check every key and it’s value and then print out another key in the same dictionary?

for key, value in diary:
    if key == "date":
        if value == "Fri, 04/11/2022":
            print(key, value)
Asked By: Dead Dog

||

Answers:

According the comments, you can iterate over the list and check for your condition. For example (lst is your list from question):

for d in lst:
    if d["studentId"] == "8426" and d["date"] == "Fri, 04/11/2022":
        print(d["assignmentId"])

Prints:

35013
35007
35005
Answered By: Andrej Kesely
diaries = [
{
    "id": "1523958",
    "studentId": "8326",
    "assignmentId": "35074",
    "diaryType": "cw",
    "diaryId": "35074",
    "subject": "URDU",
    "title": "اچھے آداب و اطوار",
    "description": "9F Maymaar - Classwork posted",
    "bRead": "1",
    "date": "Fri, 04/11/2022",
},
{
    "id": "1520938",
    "studentId": "8426",
    "assignmentId": "35013",
    "diaryType": "cw",
    "diaryId": "35013",
    "subject": "SOCIAL STUDIES",
    "title": "The Globe and Maps ",
    "description": "5C Maymaar - Classwork posted",
    "bRead": "0",
    "date": "Fri, 04/11/2022",
    "download": "2bf0c7b51af0cfb1023261dd7b8b4f8f.jpg",
},
{
    "id": "1520624",
    "studentId": "8426",
    "assignmentId": "35007",
    "diaryType": "cw",
    "diaryId": "35007",
    "subject": "MATHEMATICS",
    "title": "Percentage ",
    "description": "5C Maymaar - Classwork posted",
    "bRead": "0",
    "date": "Fri, 04/11/2022",
},
{
    "id": "1520530",
    "studentId": "8426",
    "assignmentId": "35005",
    "diaryType": "cw",
    "diaryId": "35005",
    "subject": "ENGLISH A",
    "title": "Paragraph writing ",
    "description": "5C Maymaar - Classwork posted",
    "bRead": "0",
    "date": "Fri, 04/11/2022",
},
{
    "id": "1517928",
    "studentId": "8326",
    "assignmentId": "34952",
    "diaryType": "cw",
    "diaryId": "34952",
    "subject": "PHYSICS",
    "title": "Homework",
    "description": "9F Maymaar - Classwork posted",
    "bRead": "1",
    "date": "Fri, 04/11/2022",
},
{
    "id": "1513747",
    "studentId": "8426",
    "assignmentId": "34887",
    "diaryType": "gn",
    "diaryId": "34887",
    "subject": None,
    "title": "General notice ",
    "description": "5C Maymaar - Notice posted",
    "bRead": "0",
    "date": "Thu, 03/11/2022",
},
{
    "id": "1508998",
    "studentId": "8426",
    "assignmentId": "34787",
    "diaryType": "cw",
    "diaryId": "34787",
    "subject": "SOCIAL STUDIES",
    "title": "Map reading skills ",
    "description": "5C Maymaar - Classwork posted",
    "bRead": "0",
    "date": "Thu, 03/11/2022",
},

]

for diary in diaries:
    if diary['studentId'] == '8326' and diary['date'] == 'something':
        print(diary['assignmentId'])
        
Answered By: Nbody
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.