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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:50:12+00:00 2026-05-30T22:50:12+00:00

Normally numpy forces the left and right side of an assignment to match, so

  • 0

Normally numpy forces the left and right side of an assignment to match, so for example if I do a[:] = b, b must be the same shape or broadcast to the same shape as a. But there seems to be an exception to that rule:

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

It seems to only work with 1d arrays and only if there is fancy indexing on the left side of the assignment, but I haven’t been able to find documentation for this behavior anywhere. Is this behavior documented, if so where, and also can someone give an example of when it might be useful?

Update:

It seems that the numpy flatiter type behaves this way too, is there some connection between flatiter and fancy indexing that I don’t know about?

>>> a.flat = [10,11]
>>> a
array([10, 11, 10, 11, 10, 11, 10, 11, 10, 11])
>>> a.flat[:] = [2,3,4]
>>> a
array([2, 3, 4, 2, 3, 4, 2, 3, 4, 2])
>>> a.flat = range(100)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  • 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-30T22:50:13+00:00Added an answer on May 30, 2026 at 10:50 pm

    I think this behavior is modeled on R and its ancestor S/S-plus. That’s how list assignment (“vector” assignment) works there, and it’s called “recycling”. The R project website talks about it, but I found a more illuminating explanation at this link. In R, a vector is a collection of measurements, so it makes sense to pad or trim it the way it does. How much of this logic has made it to numpy, and why, is still a good question.

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

Sidebar

Related Questions

Normally I use imagecreatefromjpeg() and then getimagesize() , but with Firefox 3 I need
Normally I create web application projects and use code-behind, but I have a requirement
Normally I would refrain from asking vague questions but in this case I cannot
Normally I restart my GAE application by right-clicking on the project and then I
Normally I'd use carrierwave, but they do not officially support jruby, and I've been
Normally I can call this.GetType(), but I can't access this in a static method.
On Windows, I normally just use the binary installer, but I would like to
Normally i've been passing variable around in init methods, but I can't do that
Normally I would do Application.Run(myMainForm). But I want to do something like this: MyForm1
Normally i am using getString(R.string.) to query strings in xml file. Is there any

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.