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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:26:12+00:00 2026-05-28T14:26:12+00:00

A question about python deepcopy and shallow copy. the post at What is the

  • 0

A question about python deepcopy and shallow copy.

the post at
What is the difference between a deep copy and a shallow copy?

cannot help me.

why e.g. 1 ‘s sum is 6 not 10 ?

e.g.1 :

kvps = { '1' : 1, '2' : 2 }
theCopy = kvps.copy()  # both point to the same mem location ? 
kvps['1'] = 5
sum = kvps['1'] + theCopy['1']
print sum

output sum is 6

e.g.2 :

aList = [1,2]
bList = [3,4]
kvps = { '1' : aList, '2' : bList }

theCopy = kvps.copy()  # both point to the same mem location ? 
kvps['1'][0] = 5
sum = kvps['1'][0] + theCopy['1'][0]

print sum

output sum is 10

e.g.3 :

import copy

aList = [1,2]
bList = [3,4]
kvps = { '1' : aList, '2' : bList }

theCopy = copy.deepcopy(kvps)
kvps['1'][0] = 5
sum = kvps['1'][0] + theCopy['1'][0]

print sum

output sum is 6.

Also , e.g. 4

kvps = { '1' : 1, '2' : 2 }    
theCopy = dict(kvps)  #  theCopy hold a reference to kvps ?     
kvps['1'] = 5  # should also change theCopy , right ?    
sum = kvps['1'] + theCopy['1']    
print kvps    
print theCopy    
print sum

its sum is 6 , if theCopy is a reference to kvps , it should be 10.

  • 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-05-28T14:26:13+00:00Added an answer on May 28, 2026 at 2:26 pm

    Shallow copy makes a copy of mutable objects in the top-level container. A deep copy makes a new instance of all mutable containers in the data structure.

    “e.g. 2” results in 10 because you copy the dict on the outside, but the two lists inside are still the old lists, and lists can be changed in-place (they’re mutable).

    Deep copy makes runs aList.copy(), bList.copy() and replaces the values in your dict with their copies.


    e.g. 1 explained:

    kvps = {'1': 1, '2': 2}
    theCopy = kvps.copy()
    
    # the above is equivalent to:
    kvps = {'1': 1, '2': 2}
    theCopy = {'1': 1, '2': 2}
    

    When you apply this to e.g. 2:

    kvps = {'1': aList, '2': bList}
    theCopy = {'1': aList, '2': bList}
    

    The list objects in both dicts are the same objects, so modifying one of the lists will be reflected in both dicts.


    Doing a deep copy (e.g. 3) results in this:

    kvps = {'1': aList, '2': bList}
    theCopy = {'1': [1, 2], '2': [3, 4]}
    

    This means both dicts have entirely different contents, and modifying one won’t modify the other.


    e.g. 4 via dict() is equivalent to a shallow copy.

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

Sidebar

Related Questions

Quick question about Python(ic) inheritence: what is the difference between the idiom posted here
first post, so play nice! I have a fairly basic question about Python dictionaries.
I have a question about sharing queues between processes in Python. Below, I have
I have a question about python mechanize's proxy support. I'm making some web client
Earlier today, I asked a question about the way Python handles certain kinds of
I have a question about using os.execvp in Python. I have the following bit
I thought about the following question about computer's architecture. Suppose I do in Python
now I am working with python. So one question about dict .... suppose I
I'm currently developing some things in Python and I have a question about variables
My question is about Python List Comprehension readability. When I come across code with

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.