Dictionary of dictionaries, each with multiple keys?

I’m trying to create a dataset to be used by Chart.js. The chart’s x-axis has 26 weekly columns for seven reaction variables. The height of each column is a number of occurrences. The expected result of the code is a dictionary containing a dictionary for each reaction with keys 0 - 25 & values for the height of each column.

The code below initializes the top level dictionary and its constituent dictionaries with key 0 also initialized. The attempt to add further keys stops at 1 and loads that key with remaining data. The code (in its test form):

Am I even close to getting where I want to go?

class ReactionTestCase(TestCase):
    def setUp(self):
        earliest = Gut.objects.order_by('happened')[0].happened
        lastDay = datetime.date.today() - datetime.timedelta(days=1)
        delta = lastDay - earliest.date()
        weeks = min(math.floor(delta.days/7), 26) # no more than 26 weeks of data
        firstDay = datetime.date.today() - datetime.timedelta(days=weeks*7)

        self.gut = Gut.objects.order_by('happened').filter(happened__gte=firstDay, happened__lte=lastDay)
        self.rxList = list(Reaction.objects.values_list('reaction', flat=True))

    def testMultiDictUpdate(self):
        g = self.gut
        dicts = {}
        j = 0 # week increment
        # create a dictionary for each reaction
        for r in range(len(self.rxList)):
            dicts[self.rxList[r]] = {}
            dicts[self.rxList[r]][j] = 0

        currDate = g[0].happened.date()
        eow = currDate + datetime.timedelta(days=7)
        for grx in g:
            rx = grx.reaction.reaction
            dicts[rx][j] += 1

            if grx.happened.date() == eow:
                eow = grx.happened.date() + datetime.timedelta(days=7)
                j += 1
                for r in range(len(self.rxList)):
                    dicts[self.rxList[r]][j] = 0
            
        print(dicts)

Test data results:

{'Barf': {0: 0, 1: 0}, 'Big D': {0: 5, 1: 33}, 'Loose': {0: 0, 1: 45}, 'Mush': {0: 2, 1: 77}, 'Nausea': {0: 0, 1: 10}, 'Normal': {0: 0, 1: 55}, 'Pain, gut': {0: 0, 1: 17}}

Do you have an example of where you want to go? That seems to me to be the first step.

To build a chart using Chart.js requires a dataset that looks like the one at pypi.org:

    def get_data(self):
        """Return 3 datasets to plot."""

        return [[75, 44, 92, 11, 44, 95, 35],
                [41, 92, 18, 3, 73, 87, 92],
                [87, 21, 94, 3, 90, 13, 65]]

Their dataset is a list of lists. My readings suggested working with dictionaries is easier than lists. The challenge with my attempt is to figure out how to accumulate data for each element so that more than two dictionary keys are populated.

You originally asked the question:

Your response implies that what you’re looking to create is:

But what you’re generating is:

So no, based on the information posted here so far, I’d say you’re heading off in the wrong direction.

However, I will also add -

That statement is not globally accurate. Different charts accept different formats of data. Which type of chart are you looking to create?

That is, at best, a misleading statement. They serve different purposes and do different things - pick the right tool for the right job.

Thanks for your reply. To “pick the right tool for the right job” requires substantially more knowledge of options than I have. I’m still pretty much at the bottom of a steep learning curve, but learning a lot along the way. Sounds like I’m too far off the target. For now. I’ll keep plodding along - just in this for the education.

It is possible that you may wish to focus more on “Python” at this point in time and less on “Django”. Trying to learn both in detail does create an extremely steep learning curve. Learning Django is a lot easier if you’re comfortable with Python.