When to use '.flat', '.flatiter' or '.flatten ()'

When it is advisable to use which of the three flattening arguments is ".flat" / '. flatiter '/'. flatten '? I know that ".flat" returns a 1D iterator over the array, does this mean that the array remains in its original form, and each element in the array can be accessed with one index (for example, using a single loop, although the array can be very dimensional ) And '.flatten' returns a full copy of the original array aligned to a 1D array.

What is less resource intensive?

+4
source share
1 answer

flatiteris just the type of the iterator object returned flat( docs ). So, all you need to know about this is that it is an iterator, like any other.

Obviously, it flattenconsumes more memory and processor, as it creates a new array, while it flatcreates an iterator object, which is very fast.

If you only need to iterate over the array, use flat.

If you need an actual flat array (for purposes other than explicitly iterating over it), use flatten.

+3
source

All Articles