[Python] How to create a numpy dtype object array from a python list without copying data?

Discussão em 'Python' iniciado por Stack, Agosto 4, 2025.

  1. Stack

    Stack Membro Participativo

    As the numpy docs describe for the object dtype, arrays created with the object dtype are simply references to an underlying data store like a python list. The tobytes() method on such an object returns pointers to this data store.

    I was wondering if it's possible to create an ndarray object from a python list without creating a copy on creation.

    For example, trying to create an ndarray from a list then assigning copy=False to np.asarray raises an exception:

    import numpy as np

    l = ['spam', 'eggs']
    arr = np.asarray(l, dtype='object', copy=False) # raises ValueError


    I don't know how numpy is storing the underlying data, but it seems like it should be very similar (if not identical) to a python list.

    Continue reading...

Compartilhe esta Página