# Error: Reducing over an empty collection is not allowed

**URL:** <https://discourse.julialang.org/t/error-reducing-over-an-empty-collection-is-not-allowed/47410>\
**Category:** New to Julia\
**Created:** [September 28, 2020, 2:52pm UTC](https://discourse.julialang.org/t/error-reducing-over-an-empty-collection-is-not-allowed/47410 "2020-09-28T14:52:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pewehweh](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@pewehweh](https://discourse.julialang.org/u/pewehweh)\
**Post date:** [September 28, 2020, 2:52pm UTC](https://discourse.julialang.org/t/error-reducing-over-an-empty-collection-is-not-allowed/47410/1 "2020-09-28T14:52:44Z")

</div>

Hi everyone,

I am trying to sort and sum data based off of the first column of ‘DNVGL\_total’ into a new array. However, when trying to do this, I get the error ‘Reducing over an empty collection is not allowed’

I am new and any help would be appreciated

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/e/4ef275a35deed22ed57b2330289581e452452cb0.png)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 28, 2020, 2:57pm UTC](https://discourse.julialang.org/t/error-reducing-over-an-empty-collection-is-not-allowed/47410/2 "2020-09-28T14:57:06Z")

</div>

It’s significantly easier to help if you post code as text instead of screenshots. You can pass the `init=0` keyword to the `sum` function to provide it with a default value in the empty case:

```julia
sum(blah blah; init=0)

```

Note that the semicolon is necessary when the argument is a generator expression to help the parser know that you’re passing a keyword argument, not iterating over something new.

---

<div class="post-metadata">

**Author:** ![pewehweh](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@pewehweh](https://discourse.julialang.org/u/pewehweh)\
**Post date:** [September 29, 2020, 6:34am UTC](https://discourse.julialang.org/t/error-reducing-over-an-empty-collection-is-not-allowed/47410/3 "2020-09-29T06:34:48Z")

</div>

DNVGL\_total =  
[  
2 1568 19 6 42 0 0 0  
4 938 47 2 45 0 0 0  
1 621 55 6 8 0 0 0  
5 571 49 5 29 21 0 0  
5 220 32 18 0 0 0 0  
3 195 13 16 8 0 0 0  
1 115 7 0 0 2 34 0  
3 103 15 3 0 0 0 0  
3 93 22 3 12 1 0 0  
3 60 10 0 0 0 0 0  
5 12 53 194 4 0 0 2  
5 6 20 89 1 0 0 0  
5 1 3 18 0 0 0 0  
5 1 0 13 0 0 0 0  
5 0 37 59 0 0 0 0  
5 0 18 16 0 0 0 1  
]  
(Y,Z) = size(DNVGL\_total)

DNVGL\_total\_sort = zeros(5,7)  
for t=1:5  
for a=1:7  
DNVGL\_total\_sort[t,a] = sum(DNVGL\_total[i,a+1] for i=1:Y if DNVGL\_total[1:Y,1] == t)  
end  
end

#I want to create a new table that sums over the rows for each column, if the value in column 1 of DNVGL\_total is equal to the row number of the new table

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 30, 2020, 4:06am UTC](https://discourse.julialang.org/t/error-reducing-over-an-empty-collection-is-not-allowed/47410/4 "2020-09-30T04:06:53Z")

</div>

You can use triple backticks ``` to write code.

What about:

```julia
julia> [a == 1 ? t : sum(DNVGL_total[findall(DNVGL_total[:,1].==t),a]) for t in 1:5, a in 1:8]
5×8 Array{Int64,2}:
 1 736 62 6 8 2 34 0
 2 1568 19 6 42 0 0 0
 3 451 60 22 20 1 0 0
 4 938 47 2 45 0 0 0
 5 811 212 412 34 21 0 3

```

(The first element of the row is unchanged.)

---

<div class="post-metadata">

**Author:** ![pewehweh](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@pewehweh](https://discourse.julialang.org/u/pewehweh)\
**Post date:** [September 30, 2020, 9:58am UTC](https://discourse.julialang.org/t/error-reducing-over-an-empty-collection-is-not-allowed/47410/5 "2020-09-30T09:58:49Z")

</div>

yes, thank you that is perfect!

---

<div class="post-metadata">

**Author:** ![mlelarge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlelarge/32/20845_2.png) [@mlelarge](https://discourse.julialang.org/u/mlelarge)\
**Post date:** [January 7, 2021, 11:52am UTC](https://discourse.julialang.org/t/error-reducing-over-an-empty-collection-is-not-allowed/47410/6 "2021-01-07T11:52:58Z")

</div>

I am using Julia 1.5.2 and this did not work: `sum([];init=0)` is giving me `ERROR: MethodError: no method matching sum(::Array{Any,1}; init=0)` but `reduce(+, []; init=0)` is working fine. Same thing with an iterator instead of an empty array.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 7, 2021, 12:07pm UTC](https://discourse.julialang.org/t/error-reducing-over-an-empty-collection-is-not-allowed/47410/7 "2021-01-07T12:07:42Z")

</div>

The `init` keyword to sum was added in 1.6, which isn’t released yet.

---

<div class="post-metadata">

**Author:** ![Zacharie\_ALES](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zacharie_ales/32/11144_2.png) [@Zacharie\_ALES](https://discourse.julialang.org/u/Zacharie_ALES)\
**Post date:** [April 13, 2021, 2:20pm UTC](https://discourse.julialang.org/t/error-reducing-over-an-empty-collection-is-not-allowed/47410/8 "2021-04-13T14:20:53Z")

</div>

Is there an alternative to “init” before version 1.6?
