# Way to filter out #undef values from list?

**URL:** https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872
**Category:** General Usage
**Created:** [September 12, 2018, 7:00pm UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872 "2018-09-12T19:00:03Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)
#### Post date: [September 12, 2018, 7:00pm UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872/1 "2018-09-12T19:00:03Z")

</div>

For example, you have:

```julia
> x = Vector(4)

4-element Array{Any,1}:
 #undef
 #undef
 #undef
 #undef

```

* * *

Now if you add some entries, you get:

```julia
> x[2] = 4
> x[4] = 7
> x

4-element Array{Any,1}:
 #undef
   4
 #undef
   7

```

* * *

How do you quickly filter out `#undef` input in one line?

```julia
#in pseudocode, 

> custom_filter(x)

2-element Array{Any,1}:
   4
   7

```

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [September 12, 2018, 7:06pm UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872/2 "2018-09-12T19:06:23Z")

</div>

```julia
filter(i -> isdefined(x, i), 1:length(x))

```

another example of why I’m really looking forward to when we have function currying in 1.1.

Not sure leaving undefined references in your array is a good practice, however. This is what `missing` was made for (or `nothing`, if you want it to throw errors).

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [September 12, 2018, 7:29pm UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872/3 "2018-09-12T19:29:47Z")

</div>

Did you actually run the code?

1. `isdefined` will not tell you anything useful here.
2. This returns the indices, not the element.

`[x[i] for i in 1:length(x) if isassigned(x, i)]` (or `Any[...]`/`eltype(x)[...]` if you need to maintain the eltype)

> [@ExpandingMan](#):
>
> why I’m really looking forward to when we have function currying in 1.1.

Even in your example that’s saving only 3 non-white space characters and harder to understand.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [September 12, 2018, 7:30pm UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872/4 "2018-09-12T19:30:30Z")

</div>

> [@ExpandingMan](#):
>
> Not sure leaving undefined references in your array is a good practice, however

And that’s the only thing you can do if you need to support `Any`.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [September 12, 2018, 7:40pm UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872/5 "2018-09-12T19:40:50Z")

</div>

Sorry, that was really stupid of me, I meant to do `x[filter(...)]` but even so yes, `isdefined` is the wrong function.

> [@yuyichao](#):
>
> And that’s the only thing you can do if you need to support `Any` .

How so?

```julia
x = Vector{Any}(missing, n)
x = Vector{Any}(nothing, n)

```

both work. Though, perhaps there’s nothing wrong with leaving them undefined.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [September 12, 2018, 7:46pm UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872/6 "2018-09-12T19:46:12Z")

</div>

> [@ExpandingMan](#):
>
> How so?

Because you are not supporting `Any` anymore. You are supporting all the types except `nothing` and `missing`.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [September 12, 2018, 7:53pm UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872/7 "2018-09-12T19:53:02Z")

</div>

> [@yuyichao](#):
>
> Because you are not supporting `Any` anymore. You are supporting all the types except `nothing` and `missing` .

Well, sure taken literally that’s certainly true, but this seems like an intended use case of `missing` or `nothing`.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [September 12, 2018, 8:02pm UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872/8 "2018-09-12T20:02:06Z")

</div>

No. If you have anything that’s no `Any` that’s fine. With any that’s not doable.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [September 12, 2018, 8:12pm UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872/9 "2018-09-12T20:12:43Z")

</div>

What I think you are saying is “You can’t do that because the OP wants the set of things which are considered ‘null’ to be the empty set’.” And I’m saying: Julia provides us with objects which are specifically intended to be considered “null”, so why not take one of those as the only element in the set of things which should be considered “null”?

I only bother to mention it because (at least in my experience) it’s not a common practice for a package to provide (as output) arrays containing undefined references and expecting someone to handle that, but it is not so unusual for them to provide arrays contianing `nothing` or `missing`.

Of course, I have no idea what the OP is trying to do, so I don’t know what the appropriate choice is here.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [September 12, 2018, 8:44pm UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872/10 "2018-09-12T20:44:58Z")

</div>

> [@ExpandingMan](#):
>
> You can’t do that because the OP wants the set of things which are considered ‘null’ to be the empty set’.”

No I didn’t. I said that you shouldn’t (edited…) consider everything about “leaving undefined references in your array” being “bad practice” (that’s what I specifically replied to). I never once mentioned what he want since he didn’t mention and I don’t want to guess. I’m only saying that you shouldn’t just see undefined reference and claim they should all be replaced by `missing` or `nothing` and I even agree that unless you need to handle `Any` (i.e. all types) `missing` or `nothing` could be fine.

---

<div class="post-metadata">

### Author: ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)
#### Post date: [December 3, 2025, 8:52am UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872/11 "2025-12-03T08:52:40Z")

</div>

Sorry for reviving this rather old thread. There has not been an accepted answer yet and I had to find an answer to exactly this question. My solution is the following set of 4 functions:

```julia
filter_undef!(x::AbstractVector) = deleteat!(x, Base.Fix1(!isassigned, x).(eachindex(x)))
filter_undef(x::AbstractArray) = deleteat!(x[:], Base.Fix1(!isassigned, x).(eachindex(x)))

function replace_undef(x::AbstractArray{T,N}, el::Tel = missing) where {T, N, Tel}
    Tnew = Union{T, Tel}
    xnew = Array{Tnew,N}(x)
    xnew[Base.Fix1(!isassigned, x).(eachindex(x))] .= el
    xnew
end

function replace_undef!(x::AbstractArray{T,N}, el::Tel = missing) where {T, N, Tel}
    Tel <: T || throw("type of $el ($Tel) does not match eltype of array ($T)")
    x[Base.Fix1(!isassigned, x).(eachindex(x))] .= el
    x
end

```

---

<div class="post-metadata">

### Author: ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)
#### Post date: [December 4, 2025, 2:59am UTC](https://discourse.julialang.org/t/way-to-filter-out-undef-values-from-list/14872/12 "2025-12-04T02:59:27Z")

</div>

> [@djsegal](#):
>
> How do you quickly filter out `#undef` input in one line?

This seems like an [XY problem](https://en.wikipedia.org/wiki/XY_problem)/asking the wrong question. `#undef` isn’t a value, it’s a label saying “this entry doesn’t exist yet”. Accessing these entries is [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior), and they can be replaced by any other value at any time (e.g. if you create an uninitialized array of floats, you’ll find a bunch of random numbers left over).

What you want to do instead is generating an array of some sentinel value (probably `nothing`), replacing some of the `nothing`s, and then filtering out any `nothing`s left behind.
