LArray.keys(self, axes=None, ascending=True)[source]

Returns a view on the array labels along axes.

axesint, str or Axis or tuple of them, optional

Axis or axes along which to iterate and in which order. Defaults to None (all axes in the order they are in the array).

ascendingbool, optional

Whether or not to iterate the axes in ascending order (from start to end). Defaults to True.


An object you can iterate (loop) on and index by position to get the Nth label along axes.


First, define a small helper function to make the following examples more readable.

>>> def str_key(key):
...     return tuple(str(k) for k in key)

Then create a test array:

>>> arr = ndtest((2, 2))
>>> arr
a\b  b0  b1
 a0   0   1
 a1   2   3

By default it iterates on all axes, in the order they are in the array.

>>> for key in arr.keys():
...     # print both the actual key object, and a (nicer) string representation
...     print(key, "->", str_key(key))
(a.i[0], b.i[0]) -> ('a0', 'b0')
(a.i[0], b.i[1]) -> ('a0', 'b1')
(a.i[1], b.i[0]) -> ('a1', 'b0')
(a.i[1], b.i[1]) -> ('a1', 'b1')
>>> for key in arr.keys(ascending=False):
...     print(str_key(key))
('a1', 'b1')
('a1', 'b0')
('a0', 'b1')
('a0', 'b0')

but you can specify another axis order:

>>> for key in arr.keys(('b', 'a')):
...     print(str_key(key))
('b0', 'a0')
('b0', 'a1')
('b1', 'a0')
('b1', 'a1')

One can specify less axes than the array has:

>>> # iterate on the "b" axis, that is return each label along the "b" axis
... for key in arr.keys('b'):
...     print(str_key(key))

One can also access elements of the key sequence directly, instead of iterating over it. Say we want to retrieve the first and last keys of our array, we could write:

>>> keys = arr.keys()
>>> first_key = keys[0]
>>> str_key(first_key)
('a0', 'b0')
>>> last_key = keys[-1]
>>> str_key(last_key)
('a1', 'b1')