# Skipnothing is missing

**URL:** <https://discourse.julialang.org/t/skipnothing-is-missing/19155>\
**Category:** General Usage\
**Created:** [December 31, 2018, 6:38pm UTC](https://discourse.julialang.org/t/skipnothing-is-missing/19155 "2018-12-31T18:38:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [December 31, 2018, 6:38pm UTC](https://discourse.julialang.org/t/skipnothing-is-missing/19155/1 "2018-12-31T18:38:24Z")

</div>

This is more of a feature request: `skipmissing` is useful, as would be `skipnothing`, if it existed. I know it’s easy to filter the `nothing` values away, but it’s also nice to drop the `Union` element type, as is achieved with `collect(skipmissing(arr))`.

I could make a PR for this, but the code would be almost identical to that underlying `skipmissing`.

---

<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:** [December 31, 2018, 6:44pm UTC](https://discourse.julialang.org/t/skipnothing-is-missing/19155/2 "2018-12-31T18:44:59Z")

</div>

I think the intention is to make `missing` stand for missing values, which propagate kind of semi-automatically when appropriate, and `nothing` an indicator of situations that there should be a value but there isn’t one and the caller should take explicit action to deal with the situation.

So I am not sure that `skipnothing` is that useful. In any case, you have

```julia
filter(!isnothing, ...)

```

which is not much longer.

See [Frequently Asked Questions · The Julia Language](https://docs.julialang.org/en/v1/manual/faq/#faq-nothing-1)

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [December 31, 2018, 7:43pm UTC](https://discourse.julialang.org/t/skipnothing-is-missing/19155/3 "2018-12-31T19:43:57Z")

</div>

> [@galenlynch](#):
>
> I know it’s easy to filter the `nothing` values away, but it’s also nice to drop the `Union` element type, as is achieved with `collect(skipmissing(arr))` .

> [@Tamas\_Papp](#):
>
> So I am not sure that `skipnothing` is that useful. In any case, you have
> 
> ```julia
> filter(!isnothing, ...)
> 
> ```
> 
> which is not much longer.

The main distinction I can see between filtering with `filter(!ismissing, ...)` and `collect(skipmissing(...))` is that the resulting collection no longer has element type `Union{Missing, ...}`. I do not know of a similarly terse way to drop the `Union` element type for `Nothing`s, when dropping the `nothing`s in a collection, which is an explicit action to deal with the situation.

---

<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:** [January 1, 2019, 9:14am UTC](https://discourse.julialang.org/t/skipnothing-is-missing/19155/4 "2019-01-01T09:14:56Z")

</div>

Try

```julia
[x for x in xs if !isnothing(x)]

```

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [January 1, 2019, 12:48pm UTC](https://discourse.julialang.org/t/skipnothing-is-missing/19155/5 "2019-01-01T12:48:43Z")

</div>

`isnothing` is not a function, but otherwise this seems like a good alternative: `[x for x in xs if x != nothing]`.

Of course, you could do the same to remove `missing` values from an iterable, and yet `skipmissing` exists as a convenient utility function. I still don’t see any difference in utility between `skipmissing`, and a potential `skipnothing` function. From the documentation, it seems like `skipmissing` is motivated to ease reducing iterables containing `missing` values without getting a `missing` result. The same problem exists when reducing iterables containing `nothing` values, except that the reduction would most likely error instead of producing a `nothing` result.

Anyway, this is just a suggestion. I personally have to deal with `nothing` values far more than I have to deal with `missing` values, and I’ve often thought that `skipnothing` would be useful.

---

<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:** [January 1, 2019, 12:51pm UTC](https://discourse.julialang.org/t/skipnothing-is-missing/19155/6 "2019-01-01T12:51:42Z")

</div>

> [@galenlynch](#):
>
> `isnothing` is not a function

Sorry, working on `master`. See

> <https://github.com/JuliaLang/julia/pull/29679>
>
> This adds an \`isnothing()\` function to \`Base\` to mirror \`ismissing()\` and \`isnan…()\` (see #29674)
> 
> I thought it would be pretty straightforward, but... I was naive.
> 
> \*\*First\*\*, I wasn't sure where to put it. Near \`notnothing()\` seemed like a good candidate, but I noticed that \`notnothing()\` is not exported. \`ismissing()\` \[is defined here\](https://github.com/JuliaLang/julia/blob/master/base/essentials.jl#L754-L769) near the definition of \`Missing\`
> 
> \`\`\`
> struct Missing end
> 
> """
> missing
> The singleton instance of type \[\`Missing\`\](@ref) representing a missing value.
> """
> const missing = Missing()
> 
> """
> ismissing(x)
> Indicate whether \`x\` is \[\`missing\`\](@ref).
> """
> ismissing(::Any) = false
> ismissing(::Missing) = true
> \`\`\`
> 
> But \`Nothing\` \[seems to be defined\](https://github.com/JuliaLang/julia/blob/master/base/boot.jl#L312) in \`boot.jl\`, which doesn't seem like the best place for this function. Any advice on location from someone that knows the code better than me (eg. anyone at all) would be appreciated. And if it's \*\*not\*\* in \`boot.jl\`, where should I export it from? (I can't find where \`ismissing()\` is exported for example). 
> 
> \*\*Second\*\*, folks on slack said that doing \`x === nothing\` was the most performant way to do this check, so I defined it as
> 
> \`\`\`
> isnothing(x::Any) = x === nothing ? true : false
> \`\`\`
> 
> But the way \`ismissing\` is defined above seems way more elegant. Should it be:
> 
> \`\`\`
> isnothing(::Any) = false
> isnothing(::Nothing) = true
> \`\`\`
> 
> instead?
> 
> \*\*Finally\*\*, I'm having trouble building julia locally, so I can't do all the steps in \[\`CONTRIBUTING.md\`\](https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md#contributing-to-core-functionality-or-base-libraries). I can't imagine how this could cause something to break, but I've been wrong with simple things before. Apologies if I'm missing something important.

> [@galenlynch](#):
>
> I’ve often thought that `skipnothing` would be useful

You could define it as a function then in your own code.

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [January 1, 2019, 12:55pm UTC](https://discourse.julialang.org/t/skipnothing-is-missing/19155/7 "2019-01-01T12:55:35Z")

</div>

I do have it in my own code. I just think it would be a useful utility for others.

Another difference is that Julia can’t infer that element type of that array comprehension:

```julia-repl
julia> a = [1, nothing];
julia> function simple_skipnothing(itr)
       [x for x in itr if x != nothing]
       end
simple_skipnothing (generic function with 1 method)
julia> @code_warntype simple_skipnothing(a)
Variables:
  #self# <optimized out>
  itr::Array{Any,1}
  #5 <optimized out>
  #6 <optimized out>
  isz <optimized out>
  et <optimized out>
  v1 <optimized out>
  st <optimized out>
  #temp#@_9 <optimized out>
  #temp#@_10::Array{_,1} where _

Body:
  begin 
      SSAValue(3) = $(Expr(:new, Base.Iterators.Filter{##6#8,Array{Any,1}}, :($(QuoteNode(#6))), :(itr)))
      SSAValue(4) = $(Expr(:new, Base.Generator{Base.Iterators.Filter{##6#8,Array{Any,1}},##5#7}, :($(QuoteNode(#5))), SSAValue(3)))
      $(Expr(:inbounds, false))
      # meta: location array.jl collect 464
      #temp#@_10::Array{_,1} where _ = $(Expr(:invoke, MethodInstance for grow_to!(::Array{Any,1}, ::Base.Generator{Base.Iterators.Filter{##6#8,Array{Any,1}},##5#7}), :(Base.grow_to!), :($(Expr(:foreigncall, :(:jl_alloc_array_1d), Array{Any,1}, svec(Any, Int64), Array{Any,1}, 0, 0, 0))), SSAValue(4)))
      goto 8 # line 471:
      8: 
      # meta: pop location
      $(Expr(:inbounds, :pop))
      return #temp#@_10::Array{_,1} where _
  end::Array{_,1} where _

```

---

<div class="post-metadata">

**Author:** ![pablosaa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosaa/32/20168_2.png) [@pablosaa](https://discourse.julialang.org/u/pablosaa)\
**Post date:** [October 6, 2021, 2:35pm UTC](https://discourse.julialang.org/t/skipnothing-is-missing/19155/8 "2021-10-06T14:35:33Z")

</div>

This works very well when x is a Vector, however in case x is an Array the resulting is a Vector. Is there a way to do it dimension-wise and keep the shape of Array?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [October 6, 2021, 2:59pm UTC](https://discourse.julialang.org/t/skipnothing-is-missing/19155/9 "2021-10-06T14:59:07Z")

</div>

There’s no way to know if each dimensional slice will contain the same number of `nothing`s, but you can filter each slice and then concatenate the equal-length slices, e.g.

```julia
julia> v = [nothing 1 0
           0 nothing 1
           1 0 nothing]
3×3 Matrix{Union{Nothing, Int64}}:
  nothing 1 0
 0 nothing 1
 1 0 nothing

julia> reduce(hcat, filter.(!isnothing, eachcol(v)))
2×3 Matrix{Union{Nothing, Int64}}:
 0 1 0
 1 0 1

```
