# How to remove empty array of a list of array

**URL:** <https://discourse.julialang.org/t/how-to-remove-empty-array-of-a-list-of-array/34790>\
**Category:** General Usage\
**Created:** [February 18, 2020, 7:18am UTC](https://discourse.julialang.org/t/how-to-remove-empty-array-of-a-list-of-array/34790 "2020-02-18T07:18:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)\
**Post date:** [February 18, 2020, 7:18am UTC](https://discourse.julialang.org/t/how-to-remove-empty-array-of-a-list-of-array/34790/1 "2020-02-18T07:18:50Z")

</div>

I have a vector that might look like these forms :

> list\_temp = Any[(Any[(“Bus1”, “Bus2"”), (“Bus2”, “Bus1”)],), (Any,)]  
> list\_temp = Any[(Any,), (Any[(“Madagascar1”, “Madagascar2”)],)]

I would like to remove any empty arrays inside my vector.  
I have tried:

`new_list=[s for s in list_temp if !isempty(s)]`

but it does not really remove empty values.  
Anyone knows how can I deal with it?  
Thanks.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 18, 2020, 7:36am UTC](https://discourse.julialang.org/t/how-to-remove-empty-array-of-a-list-of-array/34790/2 "2020-02-18T07:36:59Z")

</div>

It looks like the things you want to remove are tuples of length one containing empty arrays. You can’t test the tuple for emptiness directly since it does in fact contain an element.

```julia
julia> x = (Any[],)
(Any[],)

julia> isempty(x)
false

julia> length(x)
1

```

Instead you can use dot broadcasting to apply the emptiness test on each element of the tuple

```julia
julia> isempty.(x)
(true,)

```

and reduce it to a boolean with `all`

```julia
julia> all(isempty.(x))
true

```

This finally leads to

```julia
[s for s in list_temp if !all(isempty.(s))]

```

which might do what you want.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [February 18, 2020, 7:39am UTC](https://discourse.julialang.org/t/how-to-remove-empty-array-of-a-list-of-array/34790/3 "2020-02-18T07:39:31Z")

</div>

If you want to remove any occurence of `(Any,)` the 1-tuple holding the Julia type `Any`, that is different from wanting to remove any empty tuples (the empty tuple is `()`.  
In any event:

```julia
julia> alist = [(1,2), (), ("a", "b"), ()]
4-element Array{Tuple,1}:
 (1, 2)
 ()
 ("a", "b")
 ()

julia> blist = filter(!isempty, alist)
2-element Array{Tuple,1}:
 (1, 2)
 ("a", "b")

```

You can filter in place, too

```julia
julia> alist = [(1,2), (), ("a", "b"), ()];
julia> filter!(!isempty, alist);
julia> alist
2-element Array{Tuple,1}:
 (1, 2)
 ("a", "b")

```

and

```julia

julia> alist = [(1,2), (Any,), ("a", "b"), (Any,)]
4-element Array{Tuple{Any,Vararg{Any,N} where N},1}:
 (1, 2)
 (Any,)
 ("a", "b")
 (Any,)

julia> blist = filter(x->x!=(Any,), alist)
2-element Array{Tuple{Any,Vararg{Any,N} where N},1}:
 (1, 2)
 ("a", "b")

```
