PYTHON PROGRAMMING: What happens at back-end when you copy a List to another?

Consider we have a list ABC which has contents as [1,2,3,4] & we copy the list data to another one says XYZ. What we see after copying a list to another is that in case we make changes to XYZ they too get reflected onto ABC. Why does this happen?

 See below, after copying ABC to XYZ I have deleted the 2nd index of XYZ and fetched the XYZ & ABC details. We see the changes made in XYZ too occur in ABC

Let’s see what happens in the backend to get clarification

  • When I store a data in variable ABC it does not actually contain the values but contains a reference to that list.
  • So, when we copy ABC list to XYZ we do not copy the values but the reference link.

  • Hence when we manipulate the data using XYZ list the changes are reflected for ABC as well.
  • So, if you do not want this to happen use list keyword while copying the data.  Example:                XYZ = list (ABC)

  • Now see the below results:

Hope these details provide you a clear picture of what happens at the back-end when we copy lists in Python.

21 Comments

  1. Greetings from Idaho! I?m bored at work so I decided to check out your website on my iphone during lunch break.
    I really like the info you provide here and can?t
    wait to take a look when I get home.
    I?m shocked at how fast your blog loaded
    on my phone .. I?m not even using WIFI, just 3G
    .. Anyways, good site! https://tesgo.ca

  2. I?ve been browsing online more than 3 hours today, yet I never found any interesting article like yours.
    It is pretty
    worth enough for me. Personally,
    if all webmasters and bloggers
    made good content as you did, the internet will be a lot more useful than ever before. https://actionmobile.ca

  3. Greetings! Very helpful advice within this post! It?s the little changes which
    will make the most important changes. Thanks a lot for sharing!| I
    really love your website.. Pleasant colors &
    theme. Did you build this website yourself? Please reply back as I?m planning to
    create my own blog and would like to learn where you got this
    from or exactly what the theme is called.
    Thank you! https://clindoeilmontreal.ca

  4. Hi, Neat post. There’s an issue along with your website in internet explorer, may test this?K IE nonetheless is the market chief and a big component to other folks will leave out your excellent writing because of this problem.

Leave a Reply

Your email address will not be published.


*