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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:45:50+00:00 2026-06-18T06:45:50+00:00

If I try to change some values in an duplicated array, the original array

  • 0

If I try to change some values in an duplicated array, the original array gets mysteriously also affected.

import numpy as np

x = np.zeros((3, 10))
y = x

print(x)
print(y, "\n")

y[1:3, 4:8] = 1

print(x)
print(y)

The output on my system is as follows:

[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  1.  1.  1.  1.  0.  0.]
 [ 0.  0.  0.  0.  1.  1.  1.  1.  0.  0.]]
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  1.  1.  1.  1.  0.  0.]
 [ 0.  0.  0.  0.  1.  1.  1.  1.  0.  0.]]

I’m currently using NumPy 1.6.2 as a 64bit version compiled against the Intel MKL (from Christoph Gohlke) together with Python 3.2.3.
I also tried the 32bit “official” version but got exactly the same results…

  • 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-18T06:45:51+00:00Added an answer on June 18, 2026 at 6:45 am

    You did not create a copy of the array. You simply created a new reference to the same array. If you want to copy the array use numpy.copy:

    y = numpy.copy(x)
    

    Note that with regular python lists you can obtain a shallow copy with the syntax the_list[:](where [:] means create a slice that start at the beginning and ends at the end of the original list), while numpy slices are actually views in most cases:

    >>> import numpy as np
    a>>> a = np.arange(10)
    >>> b = a[:]
    >>> b[0] = 100
    >>> a
    array([100, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    >>> b
    array([100, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    

    Versus:

    >>> a = range(10)
    >>> b = a[:]    # does a real copy
    >>> b[0] = 100
    >>> a
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> b
    [100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    

    In python every identifier is a reference to an object. When you do an assignment:

    y = x
    

    The assignment binds the name y to the object referenced by x. In other words y becomes a copy of the reference of x. Now the objected referenced by x has one more reference. When an object doesn’t have anymore references it is deleted from memory.
    Python hasn’t got the concept of “variables” as seen in C.

    An other way of thinking is that any identifier is a pointer to an object. Thus y = x copies the pointer and not the object itself(and this is what actually happens. The C/API always uses pointers to PyObject structures)

    This is explained in every python tutorial, hence I’d suggest you to put aside numpy for a bit and read a python tutorial first.


    I’ve decided to illustrate what happens with the bind using some Unicode art.
    The idea is not mine, but of user Claudio_F from the python-it.org forum.

    The idea is that in python you have identifiers, which I’ll represent as buoys. Each buoy can be connected to an object, which is represented as a rock(or whatever) under the sea.
    An object can be bound to more buoys. When an object is not bound to any buoy it sinks in the sea and when it reaches the bottom of the sea a huge sea monster destroys it (a.k.a the garbage collector).

    Now when you do the assignment:

    a = 5
    

    This is the situation:

                 ___
                (   )
                ( a )
                (   )
    ^~~~~^~~~~^~~'¿'~~~~^~~~~~^~~~~~^~~~~~~
                  |
      O        .--+--.         o    ___
               |  5  |           |\/  x\
               .-----.            ^\_/
    
    
                 ___             O
        o       /o  \/|
                〉 GC  |       o      o
                \___/\|
            (   
       )     )             (
      (     (      )        )        )
    (  )  )  )    (   (    ( (    ) ( (
     )(  (  (   (  )   )    ) )  (   ) )
    

    When you do the assignment:

    b = a
    

    Both buoys are bound to the same object:

                ___               ___
               (   )             (   )
               ( a )             ( b )
               (   )             (   )
    ^~~~^~~~~^~~'¿'~~~~~^~~~~^~~~~'¿'~~~~^~~~~~^
                 \        o       /
                  \     o        /
                   \     O      /
                    \          /    __________
                     \        /    /          \
           o          \      /    /            \
                       \    /       )  | (  | )
         O              \  /       (   |  )  (
            o            \/            | (    )
                      .--+--.             )
                      |  5  |
                      .-----.
    
    
    
                 ___             O
        o       /o  \/|
                〉 GC  |       o      o
                \___/\|
            (   
       )     )             (
      (     (      )        )        )
    (  )  )  )    (   (    ( (    ) ( (
     )(  (  (   (  )   )    ) )  (   ) )
    

    When you do the third assignment:

    a = 7
    

    The a chain is untied from the 5 object and bound to a new, 7, object but b‘s buoy remains bound to 5:

                ___               ___
               (   )             (   )
               ( a )             ( b )
               (   )             (   )
    ^~~~^~~~~^~~'¿'~~~~~^~~~~^~~~~'¿'~~~~^~~~~~^
                 |         o       |
                 |                 |
                 |     O           |      o
                 |                 |
         o       |                 |
                 |        o        |
                 |              .--+--.
       O      .--+--.           |  5  |
              |  7  |           .-----.
              .-----.
    
         o     
    
    
    
                 ___             O
        o       /o  \/|
                〉 GC  |       o      o
                \___/\|
            (   
       )     )             (
      (     (      )        )        )
    (  )  )  )    (   (    ( (    ) ( (
     )(  (  (   (  )   )    ) )  (   ) )
    

    I hope you enjoyed the pictures and that they made clear how it works.
    (The jellyfish and the dead fish are there just because I thought they were cool)

    By the way, this representation is also awesome to understand reference between objects.
    For example the code:

    L = [1,2,3]
    

    Would be represented like this:

                    ___
                   (   )
                   ( L )
                   (   )
     ^~~~~^~~~~^~~~~'¿'~~~~^~~~~^~~~~^~~~~^
                     |
                     |
            .--------+----------.
            | [ '¿', '¿', '¿' ] |
            .----|----|----|----.
                /     |     \
               /      |      \
           .--+--.    |    .--+--.
           |  1  |    |    |  3  |
           .-----.    |    .-----.
                      |
                   .--+--.
                   |  2  |
                   .-----.
    
    
    
                 ___             O
        o       /o  \/|
                〉 GC  |       o      o
                \___/\|
            (   
       )     )             (
      (     (      )        )        )
    (  )  )  )    (   (    ( (    ) ( (
     )(  (  (   (  )   )    ) )  (   ) )
    

    Note how the list has “pointers” to the objects, and not the actual objects.

    Also, going back to slicing, if you do:

    R = L[:]
    

    You obtain this scenario:

                    ___                        ___
                   (   )                      (   )
                   ( L )                      ( R )
                   (   )                      (   )
     ^~~~~^~~~~^~~~~'¿'~~~~^~~~~^~~~~^~~~~^~~~~'¿'~~~~~~^~~~~~^~~~~~
                     |                          |
                     |                          |
            .--------+----------.     .---------+---------.
            | [ '¿', '¿', '¿' ] |     | [ '¿', '¿', '¿' ] |
            .----|----|----|----.     .----|----|----|----.
                 \     \    \             /    /    /
                  \     \    \           /    /    /
                   \     \    \         /    /    /
                    \     \    \       /    /    /
                     \     \    \     /    /    /
                      \     \    \   /    /    /
                       \     \    \ /    /    /
                        \     | .--+--. |    /
                         \    | |  1  | |   /
                          \   | .-----. |  /
                           \   \       /  /
                            \   \     /  /
                             \   \   /  /
                              |   \ /   |
                              | .--+--. |
                              | |  2  | |
                              | .-----. |
                               \       /
                                \     /
                                 \   /
                                  \ /
                                .--+--.
                                |  3  |
                                .-----.
    
    
    
                 ___             O
        o       /o  \/|
                〉 GC  |       o      o
                \___/\|
            (                                (
       )     )             (                  )   (     
      (     (      )        )        )       (  )  )     )        (
    (  )  )  )    (   (    ( (    ) ( (    )  )(  (  (  (          )(
     )(  (  (   (  )   )    ) )  (   ) )  (  (  )  )  )  )      ) (  )
    

    Note that both lists point to the same objects. That’s why it’s called shallow copy.
    If, instead of integers, you put some mutable object, like an other list, you can clearly understand why modifying it will change the contents of both lists.

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

Sidebar

Related Questions

I try to access some values in a NSMutableArray I created, but I only
I am creating some textboxes dynamically and I try to get their values when
I am developing a Firefox extension which need to change some values on a
I am creating an array for some monetary values. I created the array as
I'm trying to change some php code so that i get my values from
I have problem with c# script who change user AD password, when try change
I try to change a string into an object using eval but failed. var
I try to change the color from a UIPopoverControler on iOS 5. To do
I try to change the dimensions of the view in interface builder from the
im try to change my webrick to passenger with nginx but when i try

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.