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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:36:08+00:00 2026-05-17T20:36:08+00:00

While reading up the documentation for dict.copy() , it says that it makes a

  • 0

While reading up the documentation for dict.copy(), it says that it makes a shallow copy of the dictionary. Same goes for the book I am following (Beazley’s Python Reference), which says:

The m.copy() method makes a shallow
copy of the items contained in a
mapping object and places them in a
new mapping object.

Consider this:

>>> original = dict(a=1, b=2)
>>> new = original.copy()
>>> new.update({'c': 3})
>>> original
{'a': 1, 'b': 2}
>>> new
{'a': 1, 'c': 3, 'b': 2}

So I assumed this would update the value of original (and add ‘c’: 3) also since I was doing a shallow copy. Like if you do it for a list:

>>> original = [1, 2, 3]
>>> new = original
>>> new.append(4)
>>> new, original
([1, 2, 3, 4], [1, 2, 3, 4])

This works as expected.

Since both are shallow copies, why is that the dict.copy() doesn’t work as I expect it to? Or my understanding of shallow vs deep copying is flawed?

  • 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-17T20:36:09+00:00Added an answer on May 17, 2026 at 8:36 pm

    By “shallow copying” it means the content of the dictionary is not copied by value, but just creating a new reference.

    >>> a = {1: [1,2,3]}
    >>> b = a.copy()
    >>> a, b
    ({1: [1, 2, 3]}, {1: [1, 2, 3]})
    >>> a[1].append(4)
    >>> a, b
    ({1: [1, 2, 3, 4]}, {1: [1, 2, 3, 4]})
    

    In contrast, a deep copy will copy all contents by value.

    >>> import copy
    >>> c = copy.deepcopy(a)
    >>> a, c
    ({1: [1, 2, 3, 4]}, {1: [1, 2, 3, 4]})
    >>> a[1].append(5)
    >>> a, c
    ({1: [1, 2, 3, 4, 5]}, {1: [1, 2, 3, 4]})
    

    So:

    1. b = a: Reference assignment, Make a and b points to the same object.

      Illustration of 'a = b': 'a' and 'b' both point to '{1: L}', 'L' points to '[1, 2, 3]'.

    2. b = a.copy(): Shallow copying, a and b will become two isolated objects, but their contents still share the same reference

      Illustration of 'b = a.copy()': 'a' points to '{1: L}', 'b' points to '{1: M}', 'L' and 'M' both point to '[1, 2, 3]'.

    3. b = copy.deepcopy(a): Deep copying, a and b‘s structure and content become completely isolated.

      Illustration of 'b = copy.deepcopy(a)': 'a' points to '{1: L}', 'L' points to '[1, 2, 3]'; 'b' points to '{1: M}', 'M' points to a different instance of '[1, 2, 3]'.

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

Sidebar

Related Questions

After reading documentation on paypals developers website for quite a while, I've come to
Using the vote_fu plugin for rails 3, I'm struggling with something (that I think)
Im trying to learn Sync framework. I have followed step by step MSDN Documentation
So I'm having trouble reading how I should use the AVPlayerStatus property I have
Is it possible to manually throw a DOMException error in pure JavaScript? Documentation I've
I am trying to get started with HTK, I grabbed a copy, compiled it,
I'm trying to write my first app where in I login to website and
I've been tracking down a few memory leaks in my application. It's been a
sorry for bad Subject of topic but i couldnt find out what to write
I've gotten myself into a confused mess regarding multithreaded programming and was hoping someone

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.