# \#comprehension

**URL:** https://discourse.julialang.org/tag/comprehension/1260.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [List comprehension can't find an already defined variable](https://discourse.julialang.org/t/list-comprehension-cant-find-an-already-defined-variable/138020)

<div class="topic-metadata">

**Author:** [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Replies:** 1\
**Last updated:** [July 9, 2026, 7:12am UTC](https://discourse.julialang.org/t/list-comprehension-cant-find-an-already-defined-variable/138020 "2026-07-09T07:12:34Z")

</div>

I was dealing with an error and in the end it seems to reduce to array comprehension: c=0 z = \[1,2,3,4\] println(\[c+=i for i in z\]) it throws an error: ERROR: LoadError: UndefVarError: c not defined in local scope Su…

---

## [Multidimensional array comprehension](https://discourse.julialang.org/t/multidimensional-array-comprehension/134004)

<div class="topic-metadata">

**Author:** [@fps](https://discourse.julialang.org/u/fps)\
**Replies:** 13\
**Last updated:** [November 21, 2025, 8:31am UTC](https://discourse.julialang.org/t/multidimensional-array-comprehension/134004 "2025-11-21T08:31:59Z")

</div>

I wonder it is possible to get the following matrix with a single array comprehension that’s also efficient to evaluate: (1, 1, 1) (1, 2, 1) (1, 1, 2) (1, 2, 2) (1, 1, 3) (1, 2, 3).... (2, 1, 1) (2, 2, 1) (2, 1, 2) (2…

---

## [Metaprogramming with list comprehensions](https://discourse.julialang.org/t/metaprogramming-with-list-comprehensions/129943)

<div class="topic-metadata">

**Author:** [@rokke](https://discourse.julialang.org/u/rokke)\
**Replies:** 11\
**Last updated:** [June 17, 2025, 11:29am UTC](https://discourse.julialang.org/t/metaprogramming-with-list-comprehensions/129943 "2025-06-17T11:29:47Z")

</div>

I feel like I’m always writing \[foo(a,b,c) for a, b, c in 1:n\] when I mean \[foo(a,b,c) for a in 1:n, b in 1:n, c in 1:n\], so I’d like to add something to my startup.jl to make it so. I feel like I’ve seen conflicting in…

---

## [Memory Comprehensions](https://discourse.julialang.org/t/memory-comprehensions/129033)

<div class="topic-metadata">

**Author:** [@PatrickHaecker](https://discourse.julialang.org/u/PatrickHaecker)\
**Replies:** 7\
**Last updated:** [May 25, 2025, 10:51am UTC](https://discourse.julialang.org/t/memory-comprehensions/129033 "2025-05-25T10:51:04Z")

</div>

Is there a Memory comprehension in Julia, which I missed so far? We have Array comprehensions, but when you don’t need the multiple dimensions and resizing properties of an Array and a Memory is enough, it would be nice…

---

## [Get the transpose of Vector{Vector{Float64}}](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141)

<div class="topic-metadata">

**Author:** [@15et7dtwq](https://discourse.julialang.org/u/15et7dtwq)\
**Replies:** 9\
**Last updated:** [April 17, 2025, 8:36pm UTC](https://discourse.julialang.org/t/get-the-transpose-of-vector-vector-float64/128141 "2025-04-17T20:36:50Z")

</div>

I am coming from numpy, trying tosee if Julia can be faster. Right now I am dealing with a function that generates for me what I thought had to be a matrix, but no, it’s a vector of vector. Fine. How do I make it’s trans…

---

## [NonLinearRange](https://discourse.julialang.org/t/nonlinearrange/125221)

<div class="topic-metadata">

**Author:** [@RainerHeintzmann](https://discourse.julialang.org/u/RainerHeintzmann)\
**Replies:** 7\
**Last updated:** [January 28, 2025, 8:58am UTC](https://discourse.julialang.org/t/nonlinearrange/125221 "2025-01-28T08:58:54Z")

</div>

I was looking for way to way to allow multi-kernel broadcasting to act also over non-linear ranges. E.g. consider a complex exponential exp.(1im .\* k0 .\* x) with k0=1.35 being a scalar and x=0:255 a linear range. This …

---

## [Selecting entries of an array based on selection criteria provided by other array](https://discourse.julialang.org/t/selecting-entries-of-an-array-based-on-selection-criteria-provided-by-other-array/118632)

<div class="topic-metadata">

**Author:** [@oscarvdvelde](https://discourse.julialang.org/u/oscarvdvelde)\
**Replies:** 10\
**Last updated:** [August 28, 2024, 8:35pm UTC](https://discourse.julialang.org/t/selecting-entries-of-an-array-based-on-selection-criteria-provided-by-other-array/118632 "2024-08-28T20:35:36Z")

</div>

Consider this example, which may run in a loop where selection is updated. a = \[1.0,1.2,0.9, 3.4, 4.5,4.5, 9.7, -1.5,-2.1\] b = \[1,1,1,2,3,3,4,5,5\] # e.g. a grouping of a selection = \[1,3,5\] # these groups # What I…

---

## [Type instability in list comprehensions](https://discourse.julialang.org/t/type-instability-in-list-comprehensions/117673)

<div class="topic-metadata">

**Author:** [@mwallerb](https://discourse.julialang.org/u/mwallerb)\
**Replies:** 46\
**Last updated:** [August 9, 2024, 12:28am UTC](https://discourse.julialang.org/t/type-instability-in-list-comprehensions/117673 "2024-08-09T00:28:47Z")

</div>

Consider the following code: julia\> x = \[1, 2\]; julia\> y = Float64\[\]; julia\> \[xi \* yi for xi in x for yi in y\] Any\[\] julia\> f(x, y) = \[xi \* yi for xi in x for yi in y\]; f(x, y) Float64\[\] so the whole thing is type-uns…

---

## [String splatting inside comprehensions](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464)

<div class="topic-metadata">

**Author:** [@cnaak](https://discourse.julialang.org/u/cnaak)\
**Replies:** 11\
**Last updated:** [March 11, 2024, 5:21pm UTC](https://discourse.julialang.org/t/string-splatting-inside-comprehensions/111464 "2024-03-11T17:21:52Z")

</div>

The following excerpt (taken from a julia-1.10.2 REPL) illustrates my question: julia\> src = ("Ab","Cd") ("Ab", "Cd") julia\> \[ "Ab"..., "Cd"... \] 4-element Vector{Char}: 'A': ASCII/Unicode U+0041 (category Lu: Letter,…

---

## [Why are returned types different in comprehension over an empty range if called from a function vs top level?](https://discourse.julialang.org/t/why-are-returned-types-different-in-comprehension-over-an-empty-range-if-called-from-a-function-vs-top-level/103533)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 3\
**Last updated:** [September 5, 2023, 1:13pm UTC](https://discourse.julialang.org/t/why-are-returned-types-different-in-comprehension-over-an-empty-range-if-called-from-a-function-vs-top-level/103533 "2023-09-05T13:13:26Z")

</div>

julia\> using ApproxFun julia\> rs = Jacobi(5,4); julia\> d = domain(rs); julia\> Sf = JacobiWeight(0,2,ConstantSpace(d)); julia\> using ApproxFun.ApproxFunSingularities: jacobiweight julia\> using ApproxFun.ApproxFunBase…

---

## [Array/vector comprehension and AbstractTrees Leaves iterator](https://discourse.julialang.org/t/array-vector-comprehension-and-abstracttrees-leaves-iterator/102337)

<div class="topic-metadata">

**Author:** [@pitsianis](https://discourse.julialang.org/u/pitsianis)\
**Replies:** 0\
**Last updated:** [August 1, 2023, 1:18am UTC](https://discourse.julialang.org/t/array-vector-comprehension-and-abstracttrees-leaves-iterator/102337 "2023-08-01T01:18:45Z")

</div>

Why the array comprehension behaves differently with the AbstractTrees.Leaves iterator? In the following replacing Leaves(t) with say, range(1,3,3), I do get the 2D array. julia\> using AbstractTrees julia\> t = \[\[1, 2\]…

---

## [Slow Dict comprehension](https://discourse.julialang.org/t/slow-dict-comprehension/94797)

<div class="topic-metadata">

**Author:** [@bbejanov](https://discourse.julialang.org/u/bbejanov)\
**Replies:** 5\
**Last updated:** [February 20, 2023, 3:48pm UTC](https://discourse.julialang.org/t/slow-dict-comprehension/94797 "2023-02-20T15:48:13Z")

</div>

If I construct a Dict using a comprehension I get orders of magnitude slower speed, compared to starting with an empty Dict and running an equivalent loop to fill it in. I’m surprised, because I expected both ways to be …

---

## [Call FastClosures.jl \`@closure\` in Array Comprehensions?](https://discourse.julialang.org/t/call-fastclosures-jl-closure-in-array-comprehensions/93622)

<div class="topic-metadata">

**Author:** [@uniment](https://discourse.julialang.org/u/uniment)\
**Replies:** 2\
**Last updated:** [February 3, 2023, 1:44am UTC](https://discourse.julialang.org/t/call-fastclosures-jl-closure-in-array-comprehensions/93622 "2023-02-03T01:44:42Z")

</div>

Like with any closure, array comprehensions present a performance challenge if they capture a variable whose identifier is reassigned: julia\> using BenchmarkTools julia\> const a=\[1:1000;\]; julia\> @btime let b=a; \[b\[x\]…
