# List comprehension in Zygote

**URL:** https://discourse.julialang.org/t/list-comprehension-in-zygote/41881
**Category:** General Usage
**Tags:** zygote
**Created:** [June 22, 2020, 7:02pm UTC](https://discourse.julialang.org/t/list-comprehension-in-zygote/41881 "2020-06-22T19:02:34Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![rlrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rlrs/32/18043_2.png) [@rlrs](https://discourse.julialang.org/u/rlrs)
#### Post date: [July 1, 2020, 7:08am UTC](https://discourse.julialang.org/t/list-comprehension-in-zygote/41881/4 "2020-07-01T07:08:22Z")

</div>

While the Buffer “solution” does work, it complicates things and doesn’t make for very clean code, so I’d still like comprehension in general to work - but in most cases I still can’t make it.  
Here’s an example where I can’t make it work, although it also uses a dictionary which might add another issue to the mix:

```julia

using Zygote

w = randn(5)
grads = gradient(Params(w)) do
    dict = Dict{Int, Float64}(i => v for (i,v) in enumerate(w))
    m = maximum(i*v for (i,v) in dict)
    k = randn(5)
    sum(k .- m)
end

```

results in **MethodError: no method matching getindex(::Dict{Any,Any})**. Besides, you only get this far with the generator syntax since the array comprehension syntax allocates an array and copies data into it, which is unsupported mutation. I’ve tried a bunch of variations on the `maximum()` line to no avail.

---

_[View the full topic](https://discourse.julialang.org/t/list-comprehension-in-zygote/41881)._
