# More, less, head, tail, to display arrays in REPL

**URL:** <https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435>\
**Category:** General Usage\
**Created:** [September 28, 2020, 7:31pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435 "2020-09-28T19:31:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 28, 2020, 7:31pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/1 "2020-09-28T19:31:34Z")

</div>

Is there any functionality which mimics the `head, tail, more and less` command in bash, but to visualize the contents of arrays?

That would be quite useful, I think, for rapid inspection of data. Currently, the `Base.display()` function for arrays fills up the screen (something I find sometimes annoying when I forget to put the `;`). It would be neat if

1. one could set an environment variable to set the maximum number of lines printed by `Base.display()` of any array, and
2. be able to navigate the content of the array using `more` and `less` and equivalents.

Did someone write something like that already?

Thank you.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 28, 2020, 8:29pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/2 "2020-09-28T20:29:10Z")

</div>

One issue could be how does it work for multidimensional objects? 1D vs 2D vs 3D, etc, arrays. What does `head(A,10)` mean when `A` has 3 (or more) dimensions?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 28, 2020, 9:46pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/3 "2020-09-28T21:46:48Z")

</div>

That is true. Maybe someone can figure out something that returns meaningful and useful data in those cases. I was thinking about 2D arrays only, in reality (files are bidimensional).

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [September 28, 2020, 10:41pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/4 "2020-09-28T22:41:05Z")

</div>

Maybe a `first(x, n)` method and a `last(x, n)` method could be added to `first` and `last`, respectively. There used to be something like that in DataFrames.jl, but then they decided to improve the default printing of DataFrames instead.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 28, 2020, 10:52pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/5 "2020-09-28T22:52:13Z")

</div>

> [@CameronBieganek](#):
>
> There used to be something like that in DataFrames.jl

That actually does work for DataFrames.

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [September 28, 2020, 10:52pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/6 "2020-09-28T22:52:15Z")

</div>

Usual ordering by columns?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 28, 2020, 10:54pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/7 "2020-09-28T22:54:54Z")

</div>

Right but do you take 10 from each dimension? Or 10 from only the first dimension and don’t limit the others (this is basically what OP had in mind in 2D)? Or perhaps it should just take a tuple instead of an integer?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 28, 2020, 10:55pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/8 "2020-09-28T22:55:38Z")

</div>

Actually that is already taken care of in the usual display of multi-dimensional arrays. I do not know if it is the best solution of all, but what I am imagining here is just an extension of that display.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 29, 2020, 11:57am UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/9 "2020-09-29T11:57:22Z")

</div>

`more` / `less` would require interactivity. Specific packages exist eg for the Tables.jl ecosystem.

`head` / `tail` is useful for compact printing, but generally that is taken care of if `show` methods respect the `compact` property from the `IOContext`. When they don’t, you should open an issue.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 29, 2020, 3:16pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/10 "2020-09-29T15:16:41Z")

</div>

> [@Tamas\_Papp](#):
>
> `more` / `less` would require interactivity.

Yes. I was taking a look and this package [https://github.com/timholy/HeaderREPLs.jl/blob/master/src/HeaderREPLs.jl](https://github.com/timholy/HeaderREPLs.jl/blob/master/src/HeaderREPLs.jl) appears to provide the building blocks to start with. I will play with it when I have some time.

> [@Tamas\_Papp](#):
>
> `head` / `tail` is useful for compact printing,

True, I would like to have more control on the amount of data being printed though.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 29, 2020, 3:28pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/11 "2020-09-29T15:28:49Z")

</div>

> [@lmiq](#):
>
> more control on the amount of data being printed though

In what way? `Base.print_matrix` tries to adapt to screen size, but if you know what you are looking for, why not just print a slice using `:`?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 29, 2020, 3:32pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/12 "2020-09-29T15:32:25Z")

</div>

> [@CameronBieganek](#):
>
> `first(x, n)` method and a `last(x, n)`

That’s already implemented in the upcoming Julia 1.6.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [September 29, 2020, 4:06pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/13 "2020-09-29T16:06:04Z")

</div>

> [@Tamas\_Papp](#):
>
> `Base.print_matrix` tries to adapt to screen size

It would be nice if there were an environment variable to customize this. Often if I have the REPL full screen, I don’t want the printing of a matrix or a table to take up the entire screen.

For example, if I’m full screen, it might be nice to customize printing so that the number of rows displayed is never more than 20 (10 from the head and 10 from the tail).

Even better, the logic could be this:

- If `x` fits on the screen, then print all of `x`.
- Otherwise, print as many lines of `x` as will fit on the screen, subject to the restriction that `num_lines ≤ MAX_SHOW_LINES`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 29, 2020, 5:07pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/14 "2020-09-29T17:07:10Z")

</div>

> [@CameronBieganek](#):
>
> For example, if I’m full screen, it might be nice to customize printing so that the number of rows displayed is never more than 20 (10 from the head and 10 from the tail).

Exactly, this is one of the things I would like to have.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 30, 2020, 8:46am UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/15 "2020-09-30T08:46:22Z")

</div>

Yes, this would be nice. Perhaps check if there is an open issue about this and open one if there isn’t, so that eventually when someone redesigns printing it is considered.

That said, printing arrays is a bit tricky because the elements could be arbitrary objects with a large printed representation (even with `:compact` and `:limit` set), so it is easy to come up with corner cases.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 30, 2020, 10:58am UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/16 "2020-09-30T10:58:02Z")

</div>

It seems that the number of lines is simply read from the output of `displaysize()`. Modifying one of the “show” functions to display the minimum between that and a environment variable seems easy (it doesn’t seem to require a major redesign). For an outsider the problem is to be sure that the modification has been made in every of the many show functions that are available, consistently.

---

<div class="post-metadata">

**Author:** ![aminsadeghi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aminsadeghi/32/22853_2.png) [@aminsadeghi](https://discourse.julialang.org/u/aminsadeghi)\
**Post date:** [September 23, 2021, 2:07pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/17 "2021-09-23T14:07:52Z")

</div>

I find `numpy`’s default behaviour for printing arrays (3 rows/cols from the beginning/end of the array) to be more convenient/pretty than Julia’s `Base.display`, which fills up the entire display. What’s the reason behind the default preference in Julia?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 23, 2021, 2:55pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/18 "2021-09-23T14:55:01Z")

</div>

It all boils down to an aesthetic choice and weighting of importance between showing more data vs. preserving screen space. Preferences may and do vary — note that the original poster simultaneously wanted both more data shown and less data shown in different contexts.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 23, 2021, 3:22pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/19 "2021-09-23T15:22:30Z")

</div>

side gripe: `numpy` doesn’t show you the `eltype` though ☹

---

<div class="post-metadata">

**Author:** ![aminsadeghi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aminsadeghi/32/22853_2.png) [@aminsadeghi](https://discourse.julialang.org/u/aminsadeghi)\
**Post date:** [September 23, 2021, 6:52pm UTC](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435/20 "2021-09-23T18:52:28Z")

</div>

Thanks for weighing in. Is it possible that this is going to be revisited in future releases (if there’s enough interest by majority of the community)?

[Next page](https://discourse.julialang.org/t/more-less-head-tail-to-display-arrays-in-repl/47435.md?page=2)
