[Numpy-discussion] Why ndarray provides four ways to flatten?

Stephan Hoyer shoyer at gmail.com
Tue Oct 28 13:42:36 EDT 2014


On Tue, Oct 28, 2014 at 10:25 AM, Nathaniel Smith <njs at pobox.com> wrote:

> I too would be curious to know why .flat exists (beyond "it seemed like a
> good idea at the time" ;-)). I've always treated it as some weird legacy
> thing and ignored it, and this has worked out well for me.
>
> Is there any real problem where .flat is really the best solution? Should
> we deprecate it, or at least warn people off from it officially?
>
> .flat lets you iterate over all elements of a N-dimensional array as if it
was 1D, without ever needing to make a copy of the array. In contrast,
ravel() and reshape(-1) cannot always avoid a copy, because they need to
return another ndarray.

np.nditer is a reasonable alternative to .flat (and it's documented as
such), but it's a rather inelegant, kitchen-sink type function.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141028/11bd3f5c/attachment.html>


More information about the NumPy-Discussion mailing list