Is there an elegant way to do “not in” in Julia?

julia> a = [1, 2, 3, 4, 5];

julia> 6 ∉ a
true

The symbol can be typed in the REPL by typing \notin and then hitting TAB. Of course, the symbol is also available as an alternative to in by typing \in and hitting TAB:

julia> 6 ∈ a
false

Sometimes you need a vectorized version:

julia> x = [2, 7];

julia> x .∉ Ref(a)
2-element BitArray{1}:
 0
 1

The Ref is needed in this case so that a is treated as a scalar in the broadcasting operation.

If you prefer to avoid Unicode characters, you can write !(6 in a) instead of 6 ∉ a.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)