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 8877861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:34:16+00:00 2026-06-14T19:34:16+00:00

I am seeing some unusual behavior in Python dictionary: import numpy as np td

  • 0

I am seeing some unusual behavior in Python dictionary:

import numpy as np
td =[np.Inf, 2, 3]
a = {}
# First initialize contents of dictionary to a list of values
for k in range(10):
    a[k] = td

# now I want to access the contents to modify them based on certain criteria 
for k in range(10):
    c = a[k]
    c[0] = k
    a[k] = c

From this I would expect each first item of the list for each dictionary key value to be changed based on (c[0] = k), however, what I get at the end is that all values of the dictionary are updated to the last value of k, as in:

{0: [9, 2, 3], 1: [9, 2, 3], 2: [9, 2, 3], 3: [9, 2, 3], 
 4: [9, 2, 3], 5: [9, 2, 3], 6: [9, 2, 3], 7: [9, 2, 3], 
 8: [9, 2, 3], 9: [9, 2, 3]}

Am I missing something, or there is something wrong in the dictionary definition?

I can work around this in a different way for my code to run, but I am interested as to why the dictionary class would behave this way.

  • 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-14T19:34:17+00:00Added an answer on June 14, 2026 at 7:34 pm

    Because each key gets the same list… To make a shallow copy of the list, use the following syntax:

    for k in range(10):
        a[k] = td[:] 
    

    Demo:

    >>> d = {}
    >>> el = [1, 2, 3]
    >>> d[0] = el
    >>> d[1] = el
    >>> map(id, d.values())
    [28358416, 28358416]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am seeing some behavior I really do not understand. In the first bit
Seeing some unexpected behavior with Python tonight. Why is the following printing out 'not
I'm using MVC3 .NET4.0 (VB), and I'm seeing some strange behavior on a simple
I'm seeing some weird behavior in php when comparing a double to a string
I'm seeing some different behavior between g++ and msvc around value initializing non-copyable objects.
Seeing some strange behavior, whereby connecting to Oracle database, and then calling external function,
I am seeing some odd behavior in SQL Server AVG calcuation. On manual calculation,
I'm seeing some invalid behavior from XAML documents when the CurrentCulture is changed. When
I'm seeing some strange behavior when rounding in SQL Server 2008. Given the following
Seeing some strange behavior in my IDE and thought I'd run it by ya.

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.