0

Is there a resizable equivalent to numpy.array? Like this:

my_array.append(5)

As far as I know numpy.array has fixed size. list would be useless if my_array stores too much data. Any ideas?

Adam Jenča
  • 582
  • 7
  • 20
  • 2
    Why would `list` be useless in that case? If you know how much data you need to store, create an array of the appropriate size. If you need the data structure to grow and cannot predict how far, `list` is probably the best choice. – Grismar Sep 16 '21 at 05:38
  • Numpy arrays are allocated to have contiguous memory, that's part of the reason why it has such good performance, b/c the underlying C code will then be able fetch the data from memory in chunks and thus more efficiently use the cache. So for it to be arbitrarily resizable kind of defeats the purpose, as additional elements would almost certainly be located in another part of your process' memory space. – Jethro Cao Sep 16 '21 at 05:39
  • 3
    Also, perhaps a bit pedantic, but your title doesn't mean what you're asking - you're asking for a resizable equivalent, not a mutable one. The array is already mutable, only its size isn't, but "mutable size" is just an expensive and somewhat confusing way to say "resizable". A tuple is immutable (as an example), and a list is a mutable alternative to a tuple. – Grismar Sep 16 '21 at 05:55
  • Use an array and overallocate yourself? – no comment Sep 16 '21 at 06:13

0 Answers0