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

  • Home
  • SEARCH
  • 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 8246201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:25:35+00:00 2026-06-07T22:25:35+00:00

My attempt to programmatically create a dictionary of lists is failing to allow me

  • 0

My attempt to programmatically create a dictionary of lists is failing to allow me to individually address dictionary keys. Whenever I create the dictionary of lists and try to append to one key, all of them are updated. Here’s a very simple test case:

data = {}
data = data.fromkeys(range(2),[])
data[1].append('hello')
print data

Actual result: {0: ['hello'], 1: ['hello']}

Expected result: {0: [], 1: ['hello']}

Here’s what works

data = {0:[],1:[]}
data[1].append('hello')
print data

Actual and Expected Result: {0: [], 1: ['hello']}

Why is the fromkeys method not working as expected?

  • 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-07T22:25:38+00:00Added an answer on June 7, 2026 at 10:25 pm

    When [] is passed as the second argument to dict.fromkeys(), all values in the resulting dict will be the same list object.

    In Python 2.7 or above, use a dict comprehension instead:

    data = {k: [] for k in range(2)}
    

    In earlier versions of Python, there is no dict comprehension, but a list comprehension can be passed to the dict constructor instead:

    data = dict([(k, []) for k in range(2)])
    

    In 2.4-2.6, it is also possible to pass a generator expression to dict, and the surrounding parentheses can be dropped:

    data = dict((k, []) for k in range(2))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've decided to take the advice in this question: create-excel-chart-programmatically-in-php and NOT attempt to
I'm trying to programmatically intercept and cancel a user's attempt to check out a
I'm using the following code in an attempt to programatically allow the NetworkService account
Is there a way to using a JQuery function to programmatically create an event
How would one change the view on the screen programmatically in an iPhone app?
My Cocoa app needs some small dynamically generated windows. How can I programmatically create
In Java, we're using the following package to programmatically create excel documents: org.apache.poi.hssf If
I want to programmatically create the database create script for my EF 4.3 Code-First
First attempt to use this cool site - after searching for 2 hours: So
My attempt to customize the method contains of ArrayList, I used Eclipse to generate

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.