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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:13:25+00:00 2026-06-17T05:13:25+00:00

Possible Duplicate: Variable assignment and modification (in python) I just noticed that variable assignation

  • 0

Possible Duplicate:
Variable assignment and modification (in python)

I just noticed that variable assignation in python had some behaviour which I didn’t expect. For example:

import numpy as np
A = np.zeros([1, 3])
B = A

for ind in range(A.shape[1]):
    A[:, ind] = ind
    B[:, ind] = 2 * ind
print 'A = ', A
print 'B = ', B

outputs

A = [[ 0. 2. 4.]]

B = [[ 0. 2. 4.]]

While I was expecting:

A = [[ 0. 1. 2.]]

B = [[ 0. 2. 4.]]

If I replace “B = A” by “B = np.zeros([1, 3])”, then I have the right thing.
I can’t reproduce the unexpected result in Ipython terminal. I got that result in SciTE 3.1.0 using F5 key to run the code. I’m using Python(x, y) 2.7.2.3 distro in Win7.

  • 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-17T05:13:27+00:00Added an answer on June 17, 2026 at 5:13 am

    In your code, B is just another name for A, so when you change one you change the other. This is a common “problem” with mutable objects in Python. With numpy arrays, you can use the copy() function.

    However, this also happens with mutable containers, such as lists or dictionaries. To avoid this, you can one of one of these: (depending on the complexity of the mutable)

    B = A[:]  #makes a copy of only the first level of the mutable
    B = copy(A)  #same as above, returns a 'shallow copy' of A
    B = deepcopy(A)  #copies every element in the mutable, on every level
    

    Note that to use the copy and deepcopy functions, you need to import them from the standard module copy.

    See also: this question

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

Sidebar

Related Questions

Possible Duplicate: C# variable initializations vs assignment Just like in the title, could someone
Possible Duplicate: Global Variable from a different file Python is that possible to set
Possible Duplicate: Tuple parameter declaration and assignment oddity In Scala, one can do multiple-variable
Possible Duplicate: referenced before assignment error in python I'm getting a strange error in
Possible Duplicate: Variable length arrays in C++? I am just curious, is there any
Possible Duplicate: Why is Perl foreach variable assignment modifying the values in the array?
Possible Duplicate: Python: How do I pass a variable by reference? I'm trying to
Possible Duplicate: php variable as conditional assignment I am trying to concatenate a conditional
Possible Duplicate: Multiple assignment in Python As we have learnt right since we started
Possible Duplicate: What is the difference between slice assignment that slices the whole list

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.