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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:04:05+00:00 2026-06-18T15:04:05+00:00

In Python, with two dictionaries, simply doing dict2 = dict1 Will not cause dict2

  • 0

In Python, with two dictionaries, simply doing

dict2 = dict1

Will not cause dict2 to be a distinct copy of dict1. They will point to the same thing, so modifying dict2 will perform the same effect on dict1.

One workaround is

dict2 = dict(dict1)

So if I were to modify dict2, it would not affect dict1’s values.

In my program, I’m currently making a dictionary that’s composed of multiple copies of a previous dictionary. Let’s call the previous dictionary temp2, and the current one temp3. I don’t know how many copies I’ll need in advance, so I thought of doing this:

temp3 = {}
for i in xrange(some_number):
    temp3[i] = dict(temp2)

But my debug tests show that if I modify temp3[0]’s dictionary (which, again, is a copy of temp2), then this will also modify temp3[1]’s copy, and temp3[2], etc., so the result is a dictionary that consists of n identical copies of a dictionary, where n = some_number. Does anyone know of a workaround? Thanks.

EDIT: In response to a comment, temp2 is a dictionary composed of values that are lists, so {a: [list1], b: [list2], etc.}.

  • 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-18T15:04:06+00:00Added an answer on June 18, 2026 at 3:04 pm

    Try the copy.deepcopy method: http://docs.python.org/2/library/copy.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two dictionaries I'd like to combine in Python, i.e. a =
Im new two python and am trying to grow a dictionary of dictionaries. I
If A and B are two dictionaries, using python, is there any way of
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
For dictionaries: dict1={pk1:date1, pk2:date2, pk3:date3, pk5:date5} dict2={pk1:date11, pk3:date13, pk6:date16} I am trying to compare
I have two python dictionaries containing information about japanese words and characters: vocabDic :
In python if I have two dictionaries, specifically Counter objects that look like so
Why dictionaries in python appears reversed? >>> a = {'one': '1', 'two': '2', 'three':
Two Python strings with the same characters, a == b, may share memory, id(a)
I have two dictionaries in Python. One is a list of teams and their

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.