Click here to Skip to main content
15,668,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get loop over a query set and get values for it for different conditions and putting them in dict. Every time the loop is running it correctly adds value to dictionary but it updates previous values in dictionary. It should be only adding values not updating existing values.

def studentView(request, studentid=None):
    context = {}

    cant_assess = 0
    yes = 0
    no = 0
    somewhat_assess = 0

    label = []
    data = []
    dataset = {}
    labelset = {}

    module_query = Modules.objects.values()

    for module in module_query:
        print("For Loop 1st Level")

        query = Assessment.objects.values().filter(student = studentid)
        query = query.filter(question__modules = module['md_id'])

        for i in query:
            print('For Loop 2nd Level')
            if i['points'] == 'No':
                no = no + 1
            if i['points'] == 'somewhat':
                somewhat_assess = somewhat_assess + 1
            if i['points'] == 'cannot':
                cant_assess = cant_assess + 1
            if i['points'] == 'Yes':
                yes = yes + 1

        # print(yes)
        # print(no)
        # print(cant_assess)
        # print(somewhat_assess)

            label.append('Somewhat Assess')
            label.append('cant Assess')

            # print(data)
            print("Dataset inside loop executing for 1th query")
            dataset.update({ label[i]: data[i] for i in range(len(data))})

            labelset.update({'result_'+str(module['md_id']): dataset})

        # dataset.append(data)
        # print(dataset) 
        # print(labelset) 
        # labelset.append(label)
    # print(label)

    # context = {'dataset': dataset, 'labelset': labelset, 'detaild' : query}
    context = {'dataset': dataset, 'labelset': labelset, 'detaild' : query}

    # print(yes)
    # print(no)
    # print(cant_assess)
    # print(somewhat_assess)

    return render(request, 'student/StudentView.html', context)

What I have tried:

I have tried updating dictionary at first for loop or second for loop it doesn't help. Ideally it should be executed at second for loop level. I have tried using nested list instead of dict but its not helping. I am going to plot values from dict on a pie chart for each value. So Goal is to get multiple pie chart for each dataset. I have also tried list comprehension for updating dict in labelset

I have also tried for loop to update labelset

for i in range(len(data)):
          dataset[label[i]] = data[i]
Richard MacCutchan 21-Jun-21 4:22am    
You only have four entries in your dictionary: 'Yes', 'No', 'Somewhat Assess' and 'cant Assess'. So every time you add values they will accumulate into those labels.
Sahil Mohile 21-Jun-21 4:28am    
But they are part if different list even then they accumulate? Is there any other efficient way to get this as dict will always have unique key, list aren't helpful as i cant fetch based on key
Richard MacCutchan 21-Jun-21 4:46am    
All keys in a Dictionary must be unique. But you are collecting all your data in those four keys. Since we have no idea what data you are working on or what values you are trying to collect it is impossible to answer the question.
Sahil Mohile 21-Jun-21 5:03am    
Okay. I am trying to collect points each person is getting based from answers table where I am calculating how many times they have gotten Yes, No, Can't Assess, Somewhat Assess. This are numbers. You can see this is in 2nd Level For Loop. After that I store this info inside a variable data which is a list containing all values for how many Yes, No I got. I wish to get a graph for this data for each module(which is first for loop) this is generating a new list Data for every entry is queryset. Which then I am appending to labels another list. Then connecting both data and label to each other in form of dict called dataset. Then Finally connecting dataset to a random key to a new dict called as lableset
Richard MacCutchan 21-Jun-21 5:32am    
As I said above, you are accumulating one set of scores for all users. If you want separate sets then you need to create a dictionary for each user, and then a final dictionary of all the sets.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900