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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:40:56+00:00 2026-06-09T22:40:56+00:00

I was writing a Python Code that creates dictionaries dynamically,initializes it to a reference

  • 0

I was writing a Python Code that creates dictionaries dynamically,initializes it to a reference dictionary, and modifying a particular value in the dictionary. But,I found that not only I am getting unexpected results,but the reference dictionary is also getting modified.
My Code:

tdict={'a':'1','b':'2','c':'3'}
newdict={}
for i in range(5):
  newdict['name'+str(i)]=tdict
  newdict['name'+str(i)]['a']='value'+str(i)
  print 'tdict:  ',tdict
print 'newdict:  ',newdict

And the result:

tdict:   {'a': 'value0', 'c': '3', 'b': '2'}
tdict:   {'a': 'value1', 'c': '3', 'b': '2'}
tdict:   {'a': 'value2', 'c': '3', 'b': '2'}
tdict:   {'a': 'value3', 'c': '3', 'b': '2'}
tdict:   {'a': 'value4', 'c': '3', 'b': '2'}
newdict:   {'name4': {'a': 'value4', 'c': '3', 'b': '2'}, 'name2': {'a': 'value4', 'c': '3', 'b': '2'}, 'name3': {'a': 'value4', 'c': '3', 'b': '2'}, 'name0': {'a': 'value4', 'c': '3', 'b': '2'}, 'name1': {'a': 'value4', 'c': '3', 'b': '2'}}

whereas I expected my ‘newdict’ to be like:

newdict:   {'name4': {'a': 'value4', 'c': '3', 'b': '2'}, 'name2': {'a': 'value2', 'c': '3', 'b': '2'}, 'name3': {'a': 'value3', 'c': '3', 'b': '2'}, 'name0': {'a': 'value0', 'c': '3', 'b': '2'}, 'name1': {'a': 'value1', 'c': '3', 'b': '2'}}

Can anyone please help me figuring out why this is happening? Also, why is the reference dictionary ‘tdict’ getting changed when I am not assigning any any value to it?

Thanks in advance

  • 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-09T22:40:57+00:00Added an answer on June 9, 2026 at 10:40 pm

    You are storing a reference to tdict in every value of your newdict dictionary:

    newdict['name'+str(i)]=tdict
    

    You are then modifying the key 'a' of tdict by doing

    # newdict['name'+str(i)] is a reference to tdict
    newdict['name'+str(i)]['a']='value'+str(i)
    # this is equivalent to doing
    tdict['a']='value'+str(i)
    

    What you maybe want is storing a copy of tdict in your newdict dictionary:

    newdict['name'+str(i)]=dict(tdict)
    

    Creating a new dictionary by using an existing dictionary as constructor argument creates a shallow copy where you can assign new values to existing keys. What you cannot (or what you don’t want) is modifying mutable values in this dictionary. Example:

    >>> a={'a': 1, 'b': 2, 'c': [1,2,3]}
    >>> b=dict(a)
    >>> b['a']=9
    >>> a
    {'a': 1, 'c': [1, 2, 3], 'b': 2}
    >>> b
    {'a': 9, 'c': [1, 2, 3], 'b': 2}
    >>> b['c'].append(99)
    >>> a
    {'a': 1, 'c': [1, 2, 3, 99], 'b': 2}
    >>> b
    {'a': 9, 'c': [1, 2, 3, 99], 'b': 2}
    

    If you want to modify mutable values in a dictionary you need to create a deep copy:

    >>> import copy
    >>> a={'a': 1, 'b': 2, 'c': [1,2,3]}
    >>> b=copy.deepcopy(a)
    >>> b['a']=9
    >>> b['c'].append(99)
    >>> a
    {'a': 1, 'c': [1, 2, 3], 'b': 2}
    >>> b
    {'a': 9, 'c': [1, 2, 3, 99], 'b': 2}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any guidelines to writing Google App Engine Python code that would work
I'm writing some code (just for fun so far) in Python that will store
I feel like I spend a lot of time writing code in Python, but
python newbie here. I'm writing the code to control an experiment that has multiple
I am writing a Python program that creates a 9x9 matrix with all of
I have been writing Python code for only a couple of weeks, so I'm
I am writing Python code in Visual Studio 2010 using the excellent Python Tools
I have a problem with my python code. I am writing a GUI in
I have a programming experience with statically typed languages. Now writing code in Python
Say I have this sample code I'm writing for a Python lesson I'm holding:

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.