Not sure I understand your setup right, but anyway - once you get the data in the dict, you don't need to put the student numbers in a list. just loop over the cells with the student number in the output file and use the respective value to retrieve the correct value from the already populated dict. in the above snippet replace lines 56-65 with this one
maxRow2 = activeSheetwb2.max_row for rowNum in range(2, maxRow2 + 1): key = activeSheetwb2.cell(row=rowNum, column=1).value value = StudentNumAndScore.get(key,'') activeSheetwb2.cell(row=rowNum, column=4, value=value)