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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:55:07+00:00 2026-05-28T07:55:07+00:00

numpy has a function to set the specified elements to be some value a=numpy.zeros(10)

  • 0

numpy has a function to set the specified elements to be some value

a=numpy.zeros(10)
numpy.put(a, [2,3],1)

However, this method will directly change ‘a’ and return None if succeed.

Is there anyway we can keep ‘a’ intact and return the new array instead?

(I don’t have to use numpy, and least dependency will be more preferred)

  • 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-28T07:55:08+00:00Added an answer on May 28, 2026 at 7:55 am

    Without a function call, and in my opinion somewhat more readable, you can do this:

    >>> a = [0, 0, 0, 0, 0]
    >>> b = a[:]
    

    >>> b[1:3] = [2, 3]
    

    >>> a
    [0, 0, 0, 0, 0]
    >>> b
    [0, 2, 3, 0, 0]
    

    Note that a[:] can be very slow for large lists, because – as Makato says – it creates a completely new copy of the list. This is also the reason why numpy ships with the “in situ” (in place) function put(). If you can, avoid copying the list altogether.

    To get a sense of the performance, you can think of a[:] as [i for i in a].

    I’m not familiar with numpy, so in case I misunderstood and you want to “insert x at positions [a, b, c]”, the other way around, instead, you could do this:

    >>> def put(my_list, x, positions):
    ...     return [x if n in positions else i for n, i in enumerate(my_list)]
    ...
    >>> put(a, 1, [1, 2])
    [0, 1, 1, 0, 0]
    

    or, alternatively,

    >>> b = a[:]
    >>> b[1:3] = [1] * 2
    >>> b
    [0, 1, 1, 0, 0]
    

    Which will be slightly slower.

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

Sidebar

Related Questions

NumPy has the efficient function/method nonzero() to identify the indices of non-zero elements in
I have a list containing objects of type numpy.ndarray, all list elements has same
I am using the FFT function in NumPy to do some signal processing. I
numpy.array has a handy .tostring() method which produces a compact representation of the array
Numpy has a basic pxd that declares its c interface to cython. Is there
The numpy.equal function does not work if a list or array contains strings: >>>
I have a numpy array like this a = np.array(1) Now if I want
I originally intended to use MATLAB to tackle this problem but the in-built function
How do I print formatted NumPy arrays in a way similar to this: x
Matlab's cross-correlation function xcorr(x,y,maxlags) has an option maxlag , which returns the cross-correlation sequence

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.