In a class Week, I have created a dictionary using the .fromkey() method that contains a string key for Monday-Saturday and initializes them all to an empty list:
self.weekDict = dict.fromkeys(dayNameList, [])
I have a function that then iterates over a list of employees, and, within each iteration, iterates over their work days and appends an employee to the corresponding weekDict list of each day:
def setWeekDict(employees):
for employee in employees:
for day in employees[employee].workDays:
self.weekDict[day].append(employee)
print "employee: " + employee
print "weekday/list: " + str(day) + str(self.weekDict[day])
I expect the weekDict lists to reflect everyone who is scheduled to work on a given day. Instead, it appears that the list for each day accumulates the total number of times I've iterated over each day for the employee. My print statements output this:
employee: Addison
weekday/list: Saturday['Addison']
employee: Addison
weekday/list: Monday['Addison', 'Addison']
employee: Addison
weekday/list: Tuesday['Addison', 'Addison', 'Addison']
employee: Addison
weekday/list: Wednesday['Addison', 'Addison', 'Addison', 'Addison']
employee: Addison
weekday/list: Thursday['Addison', 'Addison', 'Addison', 'Addison', 'Addison']
Obviously I only want 'Addison' or any other employee to appear once per day, and I can't understand what in my for loop would be contributing to each list adding the employee name + the amount of times the employee name has been added to other lists before. I can only think that dict.fromkeys() creates keys that actually all point to the same list, but I didn't think that was how the fromkeys() function worked.