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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:41:54+00:00 2026-06-12T01:41:54+00:00

Are there good ways to expand a numpy ndarray? Say I have an ndarray

  • 0

Are there good ways to “expand” a numpy ndarray? Say I have an ndarray like this:

[[1 2]
 [3 4]]

And I want each row to contains more elements by filling zeros:

[[1 2 0 0 0]
 [3 4 0 0 0]]

I know there must be some brute-force ways to do so (say construct a bigger array with zeros then copy elements from old smaller arrays), just wondering are there pythonic ways to do so. Tried numpy.reshape but didn’t work:

import numpy as np
a = np.array([[1, 2], [3, 4]])
np.reshape(a, (2, 5))

Numpy complains that: ValueError: total size of new array must be unchanged

  • 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-12T01:41:55+00:00Added an answer on June 12, 2026 at 1:41 am

    There are the index tricks r_ and c_.

    >>> import numpy as np
    >>> a = np.array([[1, 2], [3, 4]])
    >>> z = np.zeros((2, 3), dtype=a.dtype)
    >>> np.c_[a, z]
    array([[1, 2, 0, 0, 0],
           [3, 4, 0, 0, 0]])
    

    If this is performance critical code, you might prefer to use the equivalent np.concatenate rather than the index tricks.

    >>> np.concatenate((a,z), axis=1)
    array([[1, 2, 0, 0, 0],
           [3, 4, 0, 0, 0]])
    

    There are also np.resize and np.ndarray.resize, but they have some limitations (due to the way numpy lays out data in memory) so read the docstring on those ones. You will probably find that simply concatenating is better.

    By the way, when I’ve needed to do this I usually just do it the basic way you’ve already mentioned (create an array of zeros and assign the smaller array inside it), I don’t see anything wrong with that!

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

Sidebar

Related Questions

If I want to expand a C macro, what are some good ways to
Are there any good ways of parsing Excel files in monotouch? Seems like most
Is there a good gem to track/log user activities on the site? Like when
Are there any 'good' ways to cause a thread waiting on a recvfrom() call
Are there any good ways to make small haskell executables? With ghc6 a simple
C programming, are there any good ways to manage path strings rather than using
Is there another way to write something like this: if (a == x ||
Are there any good ways to limit the number of times an application can
It would kind of be like S[OFU] except there will be more differences between
Are there any good ways to objectively measure a query's performance in Oracle 10g?

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.