# Sum of empty list comprehension

**URL:** https://discourse.julialang.org/t/sum-of-empty-list-comprehension/16690
**Category:** New to Julia
**Created:** [October 23, 2018, 3:59pm UTC](https://discourse.julialang.org/t/sum-of-empty-list-comprehension/16690 "2018-10-23T15:59:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ndbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndbecker/32/10829_2.png) [@ndbecker](https://discourse.julialang.org/u/ndbecker)
#### Post date: [October 23, 2018, 3:59pm UTC](https://discourse.julialang.org/t/sum-of-empty-list-comprehension/16690/1 "2018-10-23T15:59:51Z")

</div>

I’m trying to convert some python code to julia

I have a construction something like:

```julia
sum([Lc[x] for x in somelist])

```

which fails when somelist is an empty array. The python version returns 0, as I want, for the sum.

What’s the “best” (fastest?) julia replacement here?  
Is there any way to do this list comprehension and force the returned type to be e.g., Float64? That would have worked:  
sum(zeros(Float64, 0)) = 0.0

---

<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: [October 23, 2018, 4:06pm UTC](https://discourse.julialang.org/t/sum-of-empty-list-comprehension/16690/2 "2018-10-23T16:06:25Z")

</div>

The issue is what type of zero to return. For example, if you are summing a list of 3×3 matrices, you don’t want `0`, you want a 3×3 matrix of zeros.

By default, it tries to get the type of zero from the element type of the array. However, `[Lc[x] for x in []]` returns an `Any[]` array, and `Any` provides no information about the type of zero. (See also [https://github.com/JuliaLang/julia/issues/27766](https://github.com/JuliaLang/julia/issues/27766)).

One possibility would be to simply give the array comprehension an explicit type, e.g.

```julia
sum(Float64[Lc[x] for x in somelist])

```

Another argument is to use `reduce`, which allows you to specify an explicit `init` argument:

```julia
reduce(+, [Lc[x] for x in somelist], init=0.0)

```

If you are trying to write type-generic code, that could be invoked for different types of `Lc`, then you need to be more careful and use `eltype(Lc)` so that your code is both type-stable and responsive to the types of the input.

Alternatively, you can simply do

```julia
sum(Lc[somelist])

```

where `somelist` is an array of indices (possibly empty), and it will do the right thing: you can index an array with another array to get a slice/subset.

---

<div class="post-metadata">

### Author: ![ndbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndbecker/32/10829_2.png) [@ndbecker](https://discourse.julialang.org/u/ndbecker)
#### Post date: [October 23, 2018, 4:42pm UTC](https://discourse.julialang.org/t/sum-of-empty-list-comprehension/16690/3 "2018-10-23T16:42:52Z")

</div>

Thanks!

Of course the real code is more complex

```julia
Mji = [o for o in Mj if o != i]
Lq[l,j] = Lc[j]+sum(Float64[Lr[e,d[e,j]] for e in Mji])

```

where ‘d’ is a Dict, which prevents using your simple suggestion

```julia
sum(Lc[somelist])

```

I adopted your suggestion to give array comprehension a specific type, which is what I was looking for. Didn’t notice it in the docs.

---

<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: [October 23, 2018, 4:55pm UTC](https://discourse.julialang.org/t/sum-of-empty-list-comprehension/16690/4 "2018-10-23T16:55:13Z")

</div>

> [@ndbecker](#):
>
> ```julia
> Mji = [o for o in Mj if o != i]
> Lq[l,j] = Lc[j]+sum(Float64[Lr[e,d[e,j]] for e in Mji])
> 
> ```

Note that you could avoid allocating all of these temporary arrays. e.g. you can avoid the `Float64[...]` array with

```julia
Lq[l,j] = mapreduce(e -> Lr[e,d[e,j]], +, Mji, init=Lc[j])

```

> I adopted your suggestion to give array comprehension a specific type, which is what I was looking for. Didn’t notice it in the docs.

This is mentioned at the end of the comprehensions section: [Single- and multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#Comprehensions-1)

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [October 23, 2018, 5:54pm UTC](https://discourse.julialang.org/t/sum-of-empty-list-comprehension/16690/5 "2018-10-23T17:54:40Z")

</div>

> [@ndbecker](#):
>
> sum([Lc for x in somelist])

You actually don’t need the comprehension here. Ditch the square brackets and let the sum function operate on a generator and you’ll save some allocations. This is not a solution to your empty list case, though.

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [October 23, 2018, 10:32pm UTC](https://discourse.julialang.org/t/sum-of-empty-list-comprehension/16690/6 "2018-10-23T22:32:48Z")

</div>

A solution using `sum` without a temporary array:

```julia
sum(Iterators.flatten((Lc[j], Lr[e,d[e,j]] for e in Mji)))

```

(assuming `Lc[j]` is just a float, which when iterated over behaves like a 1-element container `[Lc[j]]`)
