# Empty list in sum create an error

**URL:** <https://discourse.julialang.org/t/empty-list-in-sum-create-an-error/45962>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [September 2, 2020, 4:05pm UTC](https://discourse.julialang.org/t/empty-list-in-sum-create-an-error/45962 "2020-09-02T16:05:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cotepascal76](https://avatars.discourse-cdn.com/v4/letter/c/c77e96/32.png) [@cotepascal76](https://discourse.julialang.org/u/cotepascal76)\
**Post date:** [September 2, 2020, 4:05pm UTC](https://discourse.julialang.org/t/empty-list-in-sum-create-an-error/45962/1 "2020-09-02T16:05:34Z")

</div>

I would to define a constraint in jump using this compact notation like:

```julia
sum( sum( Flow[p,j] for j in A[p] if j == "TT" ) for p in pars) )

```

the problem is that for a given p, there may be no “TT” in A[p] list and in this cas it generate an empty list and I got this error:

```julia
 ArgumentError: reducing over an empty collection is not allowed

```

There’s a solution with two for loop:

```julia
ex = AffExpr()
for p in pars
  for j in A[p]
    if j == "TT" 
      ex += Flow[p,j]

....

```

but I’m wondering if there a way keep compact fomuraltion?

Thanks

- Pascal

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 2, 2020, 4:12pm UTC](https://discourse.julialang.org/t/empty-list-in-sum-create-an-error/45962/2 "2020-09-02T16:12:24Z")

</div>

Instead of `sum( Flow[p,j] for j in A[p] if j == "TT")`, do `reduce(+, Flow[p,j] for j in A[p] if j == "TT", init=zero(eltype(Flow)))`. By providing the `init` argument, you give it a way to handle empty collections. I used `zero(eltype(Flow))` to ensure that it always returns something of the same type, since I don’t know what your `Flow` array contains.

---

<div class="post-metadata">

**Author:** ![cotepascal76](https://avatars.discourse-cdn.com/v4/letter/c/c77e96/32.png) [@cotepascal76](https://discourse.julialang.org/u/cotepascal76)\
**Post date:** [September 2, 2020, 6:05pm UTC](https://discourse.julialang.org/t/empty-list-in-sum-create-an-error/45962/4 "2020-09-02T18:05:59Z")

</div>

eltype(Flow) return _Pair{Any,Any}_ and then zero( ) function call failed with  
MethodError: no method matching zero(::Type{Pair{Any,Any}})

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 2, 2020, 6:11pm UTC](https://discourse.julialang.org/t/empty-list-in-sum-create-an-error/45962/5 "2020-09-02T18:11:31Z")

</div>

You will have to poste a full MWE for us to help you more, unfortunately.

Also, note that you quote code with backticks

```julia
`like this`

```

---

<div class="post-metadata">

**Author:** ![cotepascal76](https://avatars.discourse-cdn.com/v4/letter/c/c77e96/32.png) [@cotepascal76](https://discourse.julialang.org/u/cotepascal76)\
**Post date:** [September 2, 2020, 6:13pm UTC](https://discourse.julialang.org/t/empty-list-in-sum-create-an-error/45962/6 "2020-09-02T18:13:40Z")

</div>

Ok I find it.  
reduce(+, [Flow[p,j] for j in A[p] if j == “TT”], init=zero(1) )  
works fine.

---

<div class="post-metadata">

**Author:** ![cotepascal76](https://avatars.discourse-cdn.com/v4/letter/c/c77e96/32.png) [@cotepascal76](https://discourse.julialang.org/u/cotepascal76)\
**Post date:** [September 2, 2020, 6:14pm UTC](https://discourse.julialang.org/t/empty-list-in-sum-create-an-error/45962/7 "2020-09-02T18:14:42Z")

</div>

Ok thanks, I new here and in Julia too.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 2, 2020, 7:14pm UTC](https://discourse.julialang.org/t/empty-list-in-sum-create-an-error/45962/8 "2020-09-02T19:14:31Z")

</div>

> [@cotepascal76](#):
>
> reduce(+, [Flow[p,j] for j in A[p] if j == “TT”], init=zero(1) )

From your code above, it seems like you want `init=AffExpr()`.
