Search code examples
pythondictionarymutable

nested dictionary assignment error - confusion on mutable dictionaries


I'm having a difficult time with assignment in nested dictionaries. I have a nested dictionary with two schools, three teachers, and four students. This is just a toy example, so I don't care that the students and teachers are the same for each school. However, I do want each of the grades to be different. However, with the code below, the grades for all the students across all classes and all schools are the same as the last school / class entered.

schools = ['School A', 'School B']
teachers = ['mr. smith', 'ms. jones', 'mr. kronk']
students = ['Adam', 'Nick', 'Jeff', 'Dave']
grade_dict ={}
for i in students:
    grade_dict[i] = ''
    for j in teachers:
        teachers_dict[j] = grade_dict
        for k in schools:
            school_dict[k] = teachers_dict
for k in schools:
    for j in teachers:
        for i in students:
            a = [randint(70, 100), randint(70, 100), randint(70, 100)]
            school_dict[k][j][i] = a

This is what I want the data to look like:

School A mr. smith Adam [71, 72, 82]
School A mr. smith Nick [86, 80, 96]
School A mr. smith Jeff [77, 70, 83]
School A mr. smith Dave [79, 83, 98]
School A ms. jones Adam [70, 98, 87]
School A ms. jones Nick [80, 94, 76]
School A ms. jones Jeff [79, 82, 93]
School A ms. jones Dave [90, 97, 85]
School A mr. kronk Adam [93, 75, 95]
School A mr. kronk Nick [80, 82, 72]
School A mr. kronk Jeff [75, 72, 89]
School A mr. kronk Dave [86, 92, 98]
School B mr. smith Adam [89, 77, 84]
School B mr. smith Nick [93, 71, 74]
School B mr. smith Jeff [78, 83, 83]
School B mr. smith Dave [72, 83, 70]
School B ms. jones Adam [82, 100, 78]
School B ms. jones Nick [80, 89, 100]
School B ms. jones Jeff [91, 81, 77]
School B ms. jones Dave [86, 86, 74]
School B mr. kronk Adam [82, 73, 100]
School B mr. kronk Nick [81, 71, 74]
School B mr. kronk Jeff [92, 100, 90]
School B mr. kronk Dave [86, 97, 85]

This, unfortunately, is what the corresponding dictionary looks like:

{'School A': {'mr. kronk': {'Adam': [86, 89, 94],
   'Dave': [74, 85, 86],
   'Jeff': [79, 94, 70],
   'Nick': [90, 80, 97]},
  'mr. smith': {'Adam': [86, 89, 94],
   'Dave': [74, 85, 86],
   'Jeff': [79, 94, 70],
   'Nick': [90, 80, 97]},
  'ms. jones': {'Adam': [86, 89, 94],
   'Dave': [74, 85, 86],
   'Jeff': [79, 94, 70],
   'Nick': [90, 80, 97]}},
 'School B': {'mr. kronk': {'Adam': [86, 89, 94],
   'Dave': [74, 85, 86],
   'Jeff': [79, 94, 70],
   'Nick': [90, 80, 97]},
  'mr. smith': {'Adam': [86, 89, 94],
   'Dave': [74, 85, 86],
   'Jeff': [79, 94, 70],
   'Nick': [90, 80, 97]},
  'ms. jones': {'Adam': [86, 89, 94],
   'Dave': [74, 85, 86],
   'Jeff': [79, 94, 70],
   'Nick': [90, 80, 97]}}}

There appears to be similar questions on stackoverflow, but they couldn't quite help me solve what was going on here. Thanks.


Solution

  • You can use dictionary comprehension:

    from random import randint
    schools = ['School A', 'School B']
    teachers = ['mr. smith', 'ms. jones', 'mr. kronk']
    students = ['Adam', 'Nick', 'Jeff', 'Dave']
    
    final_dict = {school:{teacher:{student:[randint(70, 100), randint(70, 100), randint(70, 100)] for student in students} for teacher in teachers} for school in schools}
    

    Output:

    {'School A': {'mr. smith': {'Nick': [75, 81, 86], 'Dave': [85, 88, 84], 'Adam': [78, 95, 99], 'Jeff': [74, 95, 81]}, 'ms. jones': {'Nick': [76, 86, 92], 'Dave': [92, 100, 95], 'Adam': [98, 99, 90], 'Jeff': [74, 100, 95]}, 'mr. kronk': {'Nick': [84, 97, 79], 'Dave': [93, 91, 89], 'Adam': [83, 98, 79], 'Jeff': [89, 83, 99]}}, 'School B': {'mr. smith': {'Nick': [70, 78, 89], 'Dave': [81, 95, 92], 'Adam': [95, 100, 91], 'Jeff': [91, 83, 82]}, 'ms. jones': {'Nick': [94, 85, 75], 'Dave': [99, 77, 94], 'Adam': [79, 97, 92], 'Jeff': [91, 84, 79]}, 'mr. kronk': {'Nick': [81, 90, 86], 'Dave': [72, 95, 82], 'Adam': [80, 73, 77], 'Jeff': [88, 88, 95]}}}