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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:35:35+00:00 2026-05-13T12:35:35+00:00

I generate a list of one dimensional numpy arrays in a loop and later

  • 0

I generate a list of one dimensional numpy arrays in a loop and later convert this list to a 2d numpy array. I would’ve preallocated a 2d numpy array if i knew the number of items ahead of time, but I don’t, therefore I put everything in a list.

The mock up is below:

>>> list_of_arrays = map(lambda x: x*ones(2), range(5))
>>> list_of_arrays
[array([ 0.,  0.]), array([ 1.,  1.]), array([ 2.,  2.]), array([ 3.,  3.]), array([ 4.,  4.])]
>>> arr = array(list_of_arrays)
>>> arr
array([[ 0.,  0.],
       [ 1.,  1.],
       [ 2.,  2.],
       [ 3.,  3.],
       [ 4.,  4.]])

My question is the following:

Is there a better way (performancewise) to go about the task of collecting sequential numerical data (in my case numpy arrays) than putting them in a list and then making a numpy.array out of it (I am creating a new obj and copying the data)? Is there an “expandable” matrix data structure available in a well tested module?

A typical size of my 2d matrix would be between 100×10 and 5000×10 floats

EDIT: In this example i’m using map, but in my actual application I have a for loop

  • 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-13T12:35:36+00:00Added an answer on May 13, 2026 at 12:35 pm

    Suppose you know that the final array arr will never be larger than 5000×10.
    Then you could pre-allocate an array of maximum size, populate it with data as
    you go through the loop, and then use arr.resize to cut it down to the
    discovered size after exiting the loop.

    The tests below suggest doing so will be slightly faster than constructing intermediate
    python lists no matter what the ultimate size of the array is.

    Also, arr.resize de-allocates the unused memory, so the final (though maybe not the intermediate) memory footprint is smaller than what is used by python_lists_to_array.

    This shows numpy_all_the_way is faster:

    % python -mtimeit -s"import test" "test.numpy_all_the_way(100)"
    100 loops, best of 3: 1.78 msec per loop
    % python -mtimeit -s"import test" "test.numpy_all_the_way(1000)"
    100 loops, best of 3: 18.1 msec per loop
    % python -mtimeit -s"import test" "test.numpy_all_the_way(5000)"
    10 loops, best of 3: 90.4 msec per loop
    
    % python -mtimeit -s"import test" "test.python_lists_to_array(100)"
    1000 loops, best of 3: 1.97 msec per loop
    % python -mtimeit -s"import test" "test.python_lists_to_array(1000)"
    10 loops, best of 3: 20.3 msec per loop
    % python -mtimeit -s"import test" "test.python_lists_to_array(5000)"
    10 loops, best of 3: 101 msec per loop
    

    This shows numpy_all_the_way uses less memory:

    % test.py
    Initial memory usage: 19788
    After python_lists_to_array: 20976
    After numpy_all_the_way: 20348
    

    test.py:

    import numpy as np
    import os
    
    
    def memory_usage():
        pid = os.getpid()
        return next(line for line in open('/proc/%s/status' % pid).read().splitlines()
                    if line.startswith('VmSize')).split()[-2]
    
    N, M = 5000, 10
    
    
    def python_lists_to_array(k):
        list_of_arrays = list(map(lambda x: x * np.ones(M), range(k)))
        arr = np.array(list_of_arrays)
        return arr
    
    
    def numpy_all_the_way(k):
        arr = np.empty((N, M))
        for x in range(k):
            arr[x] = x * np.ones(M)
        arr.resize((k, M))
        return arr
    
    if __name__ == '__main__':
        print('Initial memory usage: %s' % memory_usage())
        arr = python_lists_to_array(5000)
        print('After python_lists_to_array: %s' % memory_usage())
        arr = numpy_all_the_way(5000)
        print('After numpy_all_the_way: %s' % memory_usage())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to generate a list of files within a directory. Some of
I want to generate a list in C#. I am missing python's list comprehensions.
I need to generate a list of sequential numbers. I know Ruby you can
I need to generate a list of activity from multiple tables in order of
Is it possible to generate a list of all source members within an iSeries
Our team is using a SecureRandom to generate a list of key pairs (the
I'm working on a small app where I can generate a list of barcodes.
I tried to use OPTION (MAXRECURSION 0) in a view to generate a list
Say you want to generate a matched list of identifiers and strings enum {
How do I generate all the permutations of a list? For example: permutations([]) []

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.