Skip to content
Advertisement

Nested dictionary incorrectly populating all top level key-value pairs with same values

I am having a strange issue where indexing a nested dictionary correctly results in the same value being assigned for all top level keys (k’s). Below is the whole chunk of code.

    scores_by_n = dict.fromkeys(range(3, 5 + 1), {})
    for k in range(3, 5 + 1):  # this is the initial sample from the population of 5 (min of 3)
        for j in range(1, k + 1):  # sample of the initial sample of the population
            print(k, j)
            for i in [50,100]:  # how many times to repeatedly sample the sample
                try:
                    scores_by_n[k][j][i] = {'E-6':3, "E-7":9, "E-8":100}
                    print(scores_by_n)
                except KeyError:
                    avg, stdev = ({'E-6':1111, "E-7":1111, "E-8":1111}, {'E-6':3333, "E-7":3333, "E-8":3333})
                    scores_by_n[k][j] = {i: avg}
                    print(scores_by_n)

Below is the output of this block:

3 1
{3: {1: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}}}, 4: {1: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}}}}
{3: {1: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}, 4: {1: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}}
3 2
{3: {1: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}}}, 4: {1: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}}}}
{3: {1: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}, 4: {1: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}}
3 3
{3: {1: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}}}, 4: {1: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}}}}
{3: {1: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}, 4: {1: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}}
4 1
{3: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}, 4: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}}
{3: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}, 4: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}}
4 2
{3: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}, 4: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}}
{3: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}, 4: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}}
4 3
{3: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}, 4: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}}
{3: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}, 4: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}}
4 4
{3: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 4: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}}}, 4: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 4: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}}}}
{3: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 4: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}, 4: {1: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 2: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 3: {50: {'E-6': 3, 'E-7': 9, 'E-8': 100}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}, 4: {50: {'E-6': 1111, 'E-7': 1111, 'E-8': 1111}, 100: {'E-6': 3, 'E-7': 9, 'E-8': 100}}}}

You can see by the print statements that after the first iteration of the middle loop, all top level keys have been assigned the same values, even though the line reads as scores_by_n[k][j]. Please help me understand how this issue is occurring.

Advertisement

Answer

Create new dict by this instead.

scores_by_n = {k: {} for k in range(3, 5 + 1)}

The issue is caused by using the fromkeys() function to create the outermost dictionary and specifying the same dictionary object as the value for all keys. This means that all keys in the outermost refer to the same dictionary object, so any changes made to the object were reflected for all keys.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement