Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9102743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:27:14+00:00 2026-06-17T01:27:14+00:00

In a class Week, I have created a dictionary using the .fromkey() method that

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:27:16+00:00Added an answer on June 17, 2026 at 1:27 am

    fromkeys() if used with mutable objects can lead to such behavior because it assigns the same object(same id()) to every key of the dictionary.

    In [16]: dic=dict.fromkeys("spam",[])
    
    In [17]: [id(x) for x in dic.values()]    #all of them are same objects
    Out[17]: [141885196, 141885196, 141885196, 141885196]
    
    In [18]: dic['s'].append(1)   #changing one changes others as well
    
    In [19]: dic
    Out[19]: {'a': [1], 'm': [1], 'p': [1], 's': [1]}
    

    So, dict-comprehensions must be used with mutable objects:

    In [20]: dic={x:[] for x in "spam"}
    
    In [21]: [id(x) for x in dic.values()]               #unique objects
    Out[21]: [141884972, 141848300, 142262988, 141895980]
    
    In [22]: dic['s'].append(1)
    
    In [23]: dic
    Out[23]: {'a': [], 'm': [], 'p': [], 's': [1]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python, I have a Graph class that has a dictionary of vertex objects.
I have created the following class that extends JSpinner to iterate over dd/mm/yyy values.
Last week, we created a program that manages sets of strings, using classes and
I have a djano model: class Expiration(models.Model): UNIT_CHOICES = ( ('M', 'Month'), ('W', 'Week')
Here is my problem: The Problem I have created Tabs using jQuery which is
I have Week model that has Day as child objects. There is days relation
I have created a simple calendar using GridView and whenever I switch from portrait
I started using NHibernate this week (struggling). I have a small application with 3
I'm writing a program consisting of dynamically created panels that each have a few
I have a Portable Class Library that targets .NET for Windows Store apps and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.