# \#for-loop

**URL:** https://discourse.julialang.org/tag/for-loop/909.md

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

---

## [Is it defined behavior to modify \`iter\` while \`for\`-looping over it?](https://discourse.julialang.org/t/is-it-defined-behavior-to-modify-iter-while-for-looping-over-it/132477)

<div class="topic-metadata">

**Author:** [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Replies:** 32\
**Last updated:** [October 6, 2025, 11:15am UTC](https://discourse.julialang.org/t/is-it-defined-behavior-to-modify-iter-while-for-looping-over-it/132477 "2025-10-06T11:15:32Z")

</div>

Since the docstring of for is very concise, I cannot fathom one thing—as the title suggests. for i = iter # body end I have learned that i is local and different among passes. For example, iter = 1:3, then we have…

---

## [Different modification behavior when looping over array of variables of different array types](https://discourse.julialang.org/t/different-modification-behavior-when-looping-over-array-of-variables-of-different-array-types/132243)

<div class="topic-metadata">

**Author:** [@chaseleslie](https://discourse.julialang.org/u/chaseleslie)\
**Replies:** 8\
**Last updated:** [September 10, 2025, 4:57pm UTC](https://discourse.julialang.org/t/different-modification-behavior-when-looping-over-array-of-variables-of-different-array-types/132243 "2025-09-10T16:57:41Z")

</div>

Example code: function filterarrays!(d) for arr in \[d\["a"\], d\["b"\]\] deleteat!(arr, 2) end end d = Dict("a" =\> \[1, 2, 3\], "b" =\> \[1., 2., 3.\]) display(d) filterarrays!(d) display(d) With output: Dict{S…

---

## [Huge performance variance with \`if\` options in loops](https://discourse.julialang.org/t/huge-performance-variance-with-if-options-in-loops/130462)

<div class="topic-metadata">

**Author:** [@hz-xiaxz](https://discourse.julialang.org/u/hz-xiaxz)\
**Replies:** 4\
**Last updated:** [July 4, 2025, 6:51am UTC](https://discourse.julialang.org/t/huge-performance-variance-with-if-options-in-loops/130462 "2025-07-04T06:51:35Z")

</div>

I’m trying to optimize a non-allocating matrix update. Here is the simple code. function update\_W\_original!( W::AbstractMatrix, l::Int, K::Int, col\_cache::AbstractVector, row\_cache::AbstractVector, )…

---

## [Nested loop in @mtkmodel / Matrix manipulation](https://discourse.julialang.org/t/nested-loop-in-mtkmodel-matrix-manipulation/130133)

<div class="topic-metadata">

**Author:** [@madtorresga](https://discourse.julialang.org/u/madtorresga)\
**Replies:** 2\
**Last updated:** [June 23, 2025, 4:56pm UTC](https://discourse.julialang.org/t/nested-loop-in-mtkmodel-matrix-manipulation/130133 "2025-06-23T16:56:09Z")

</div>

Hi :slight\_smile: I’m trying to automate the battery cells’ creation from an already defined ECM model: const Qs = \[4.8 4.4 4.2 ; 3.7 3.5 3.3\] @mtkmodel Series\_Connection begin @variables begin v\_max …

---

## [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…

---

## [What is the advantage of "foreach(v) do.." over "for i in v..."?](https://discourse.julialang.org/t/what-is-the-advantage-of-foreach-v-do-over-for-i-in-v/129007)

<div class="topic-metadata">

**Author:** [@Leon\_Niceday](https://discourse.julialang.org/u/Leon_Niceday)\
**Replies:** 12\
**Last updated:** [May 16, 2025, 1:15pm UTC](https://discourse.julialang.org/t/what-is-the-advantage-of-foreach-v-do-over-for-i-in-v/129007 "2025-05-16T13:15:57Z")

</div>

This julia\> let t = 0 for x in v t += x end t end vs julia\> let t = 0 foreach(v) do x t += x end t end It …

---

## [Experiments with LoopVectorization and convolutions](https://discourse.julialang.org/t/experiments-with-loopvectorization-and-convolutions/123188)

<div class="topic-metadata">

**Author:** [@Marco\_Lombardi](https://discourse.julialang.org/u/Marco_Lombardi)\
**Replies:** 24\
**Last updated:** [December 3, 2024, 8:35pm UTC](https://discourse.julialang.org/t/experiments-with-loopvectorization-and-convolutions/123188 "2024-12-03T20:35:27Z")

</div>

I have been doing some experiments with the great LoopVectorization for convolution, and I must admit that in spite of my efforts to manually refactor my code, I have been unable to even approach the speed that this pack…

---

## [Help with for-loops optimization / Translation from Matlab](https://discourse.julialang.org/t/help-with-for-loops-optimization-translation-from-matlab/118204)

<div class="topic-metadata">

**Author:** [@Benjamin7](https://discourse.julialang.org/u/Benjamin7)\
**Replies:** 7\
**Last updated:** [August 15, 2024, 7:42am UTC](https://discourse.julialang.org/t/help-with-for-loops-optimization-translation-from-matlab/118204 "2024-08-15T07:42:59Z")

</div>

Hi, I have been asked to translate a matlab code to Julia to gain speed, what I have done is to put each for loop with the @inbound, put each array and all important code in a function, I have defined the type of almost …

---

## [Make ProgressMeter description display depends on a for-loop variable](https://discourse.julialang.org/t/make-progressmeter-description-display-depends-on-a-for-loop-variable/117509)

<div class="topic-metadata">

**Author:** [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Replies:** 3\
**Last updated:** [July 27, 2024, 12:49pm UTC](https://discourse.julialang.org/t/make-progressmeter-description-display-depends-on-a-for-loop-variable/117509 "2024-07-27T12:49:53Z")

</div>

The following works well: using ProgressMeter @showprogress dt=1 desc="Computing ..." for c in 1:100\_000 # Some computing end However, I’d like the description to display some string that depends on c: using Progr…

---

## [Reduce allocation in nested for loops](https://discourse.julialang.org/t/reduce-allocation-in-nested-for-loops/117428)

<div class="topic-metadata">

**Author:** [@EthanN](https://discourse.julialang.org/u/EthanN)\
**Replies:** 2\
**Last updated:** [July 24, 2024, 8:46pm UTC](https://discourse.julialang.org/t/reduce-allocation-in-nested-for-loops/117428 "2024-07-24T20:46:29Z")

</div>

I have a function that calculates the expectation and covariance of random variables on a grid with dimensions (nx, ny, nz). The variables y and z are Markovian, with Qy and Qz being their Markov matrices. Functions f an…

---

## [Variable Scoping in Non-Interactive Context](https://discourse.julialang.org/t/variable-scoping-in-non-interactive-context/115603)

<div class="topic-metadata">

**Author:** [@astro-kevin](https://discourse.julialang.org/u/astro-kevin)\
**Replies:** 1\
**Last updated:** [June 13, 2024, 2:53pm UTC](https://discourse.julialang.org/t/variable-scoping-in-non-interactive-context/115603 "2024-06-13T14:53:56Z")

</div>

I’m trying to index a vector in a soft local variable scope, but I just can’t seem to do it. The code looks minimally something like this: in a file called testfile.jl struct TestStruct thing::Int64 end function Fo…

---

## [Replacing components with indices](https://discourse.julialang.org/t/replacing-components-with-indices/113412)

<div class="topic-metadata">

**Author:** [@KeepLearning](https://discourse.julialang.org/u/KeepLearning)\
**Replies:** 12\
**Last updated:** [April 25, 2024, 4:00pm UTC](https://discourse.julialang.org/t/replacing-components-with-indices/113412 "2024-04-25T16:00:55Z")

</div>

I am wondering if the following can be done in a better way (without for loop, perhaps): given a vector and a vector of vectors, e.g. v = \[1, 2, 3, 3, 4, 4, 5\] u = \[\[1,2\], \[3, 5\], \[4\]\] I like to get \[1, 1, 2, 2, 3, 3,…

---

## [Is it safe to pop! a Dict in a loop like this?](https://discourse.julialang.org/t/is-it-safe-to-pop-a-dict-in-a-loop-like-this/108441)

<div class="topic-metadata">

**Author:** [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Replies:** 15\
**Last updated:** [January 7, 2024, 9:47pm UTC](https://discourse.julialang.org/t/is-it-safe-to-pop-a-dict-in-a-loop-like-this/108441 "2024-01-07T21:47:43Z")

</div>

At the moment, the following code gives the result that I want (i.e. the for loop goes through each key in the dict once): let mydict = Dict(\[i =\> rand() for i in 1:10\]) println(mydict) for k in keys(mydict) …

---

## [Redefining print method changes variable scoping in loop](https://discourse.julialang.org/t/redefining-print-method-changes-variable-scoping-in-loop/108010)

<div class="topic-metadata">

**Author:** [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)\
**Replies:** 9\
**Last updated:** [January 5, 2024, 2:20pm UTC](https://discourse.julialang.org/t/redefining-print-method-changes-variable-scoping-in-loop/108010 "2024-01-05T14:20:18Z")

</div>

Why does the first loop work but the second does not? The MWE: Summaryusing Printf for n in 1:10 if n == 1 flag = true idx\_last = 1 else flag = false idx\_last = ifelse(flag, n, idx\_last) end # P…

---

## [Loop performance issue for a specific data size](https://discourse.julialang.org/t/loop-performance-issue-for-a-specific-data-size/107021)

<div class="topic-metadata">

**Author:** [@ReWoP](https://discourse.julialang.org/u/ReWoP)\
**Replies:** 2\
**Last updated:** [December 7, 2023, 6:10pm UTC](https://discourse.julialang.org/t/loop-performance-issue-for-a-specific-data-size/107021 "2023-12-07T18:10:07Z")

</div>

Hello everyone! I’m testing a population-based algorithm and have encountered performance issues, particularly when looping with large populations (i.e., using large data matrices). Everything works fine when using mat…

---

## [How to improve code to compute the mean of values grouped by DateTime?](https://discourse.julialang.org/t/how-to-improve-code-to-compute-the-mean-of-values-grouped-by-datetime/106235)

<div class="topic-metadata">

**Author:** [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Replies:** 7\
**Last updated:** [November 15, 2023, 11:27pm UTC](https://discourse.julialang.org/t/how-to-improve-code-to-compute-the-mean-of-values-grouped-by-datetime/106235 "2023-11-15T23:27:37Z")

</div>

I have written the piece of code below. It takes a vector of DateTime, a vector of Float64, and an integer g as input to calculate the mean of the the type Float64 by grouped DateTime type given time granularity g. In t…

---

## [Return the ith iteration value of a variable in a for loop when the broken from the outside using @async](https://discourse.julialang.org/t/return-the-ith-iteration-value-of-a-variable-in-a-for-loop-when-the-broken-from-the-outside-using-async/100301)

<div class="topic-metadata">

**Author:** [@phantom](https://discourse.julialang.org/u/phantom)\
**Replies:** 7\
**Last updated:** [June 15, 2023, 1:10am UTC](https://discourse.julialang.org/t/return-the-ith-iteration-value-of-a-variable-in-a-for-loop-when-the-broken-from-the-outside-using-async/100301 "2023-06-15T01:10:13Z")

</div>

Hi sorry if I am being dense here. I would like the option of terminating a for loop from outside the function. If the loop is completed it should return the value of the variable being modified. However if the loop …

---

## [How to globally change a data frame inside for loop?](https://discourse.julialang.org/t/how-to-globally-change-a-data-frame-inside-for-loop/99361)

<div class="topic-metadata">

**Author:** [@Yao\_Vang](https://discourse.julialang.org/u/Yao_Vang)\
**Replies:** 4\
**Last updated:** [May 26, 2023, 6:06pm UTC](https://discourse.julialang.org/t/how-to-globally-change-a-data-frame-inside-for-loop/99361 "2023-05-26T18:06:16Z")

</div>

Hi there! How to apply changes in for loops to be kept outside of for loop too? I have a dataframe that is read inside a for loop via a function and and after bunch of computation, I apply a filter command over it and …

---

## [OpenStreetMapX: How to efficiently convert node\_id to vertex\_id?](https://discourse.julialang.org/t/openstreetmapx-how-to-efficiently-convert-node-id-to-vertex-id/98139)

<div class="topic-metadata">

**Author:** [@Yao\_Vang](https://discourse.julialang.org/u/Yao_Vang)\
**Replies:** 2\
**Last updated:** [May 1, 2023, 9:05am UTC](https://discourse.julialang.org/t/openstreetmapx-how-to-efficiently-convert-node-id-to-vertex-id/98139 "2023-05-01T09:05:24Z")

</div>

Hello all When calling any routing opetion in openstreetmapX it expects node\_id and in the answer it also returns node\_id. for example: m = get\_map\_data(File\_path, use\_cache=false, trim\_to\_connected\_graph=true ); a,b =…

---

## [If-else loop failure for a large array](https://discourse.julialang.org/t/if-else-loop-failure-for-a-large-array/97180)

<div class="topic-metadata">

**Author:** [@Sh\_166](https://discourse.julialang.org/u/Sh_166)\
**Replies:** 5\
**Last updated:** [April 7, 2023, 8:22am UTC](https://discourse.julialang.org/t/if-else-loop-failure-for-a-large-array/97180 "2023-04-07T08:22:24Z")

</div>

I have the following code: if sol\[5,i\] - sol\_1\[5,i\] \>= 0.0 s\[i\] = sol\[5,i\] - sol\_1\[5,i\] else s\[i\] = sol\_1\[5,i\] - sol\[5,i\] end end Now, here, sol and sol\_1 correspond to 2 solutions I have fr…

---

## [Trouble using for loop for SARIMA](https://discourse.julialang.org/t/trouble-using-for-loop-for-sarima/91519)

<div class="topic-metadata">

**Author:** [@Agnes\_H](https://discourse.julialang.org/u/Agnes_H)\
**Replies:** 1\
**Last updated:** [December 11, 2022, 2:30am UTC](https://discourse.julialang.org/t/trouble-using-for-loop-for-sarima/91519 "2022-12-11T02:30:31Z")

</div>

Hi, I apologize in advance, most of my programming skills are self-taught, if there are any weird errors. My goal with this code was to basically create a for-loop that iterates over every column in my data frame separa…

---

## [How to stop a loop without loosing the generated data?](https://discourse.julialang.org/t/how-to-stop-a-loop-without-loosing-the-generated-data/90936)

<div class="topic-metadata">

**Author:** [@Alex\_ricci](https://discourse.julialang.org/u/Alex_ricci)\
**Replies:** 2\
**Last updated:** [November 28, 2022, 6:31am UTC](https://discourse.julialang.org/t/how-to-stop-a-loop-without-loosing-the-generated-data/90936 "2022-11-28T06:31:36Z")

</div>

I work on an iterative algorithm. The simplified version of it is Step 1: optimize problem1 Step 2: if objective\_function\_of\_problem1 \> zub exclude the answer and go to the step 1 Step 3: optimize problem2 with data p…

---

## [How to access/ change a parameter inside loop?](https://discourse.julialang.org/t/how-to-access-change-a-parameter-inside-loop/88747)

<div class="topic-metadata">

**Author:** [@Mike\_comen](https://discourse.julialang.org/u/Mike_comen)\
**Replies:** 5\
**Last updated:** [October 14, 2022, 7:26pm UTC](https://discourse.julialang.org/t/how-to-access-change-a-parameter-inside-loop/88747 "2022-10-14T19:26:13Z")

</div>

As variables are local inside while/ for loop in Julia, I was wondering how to see the changes that happen inside loop and then use the values after changes?? For example, consider a very simple implementation of raisin…

---

## [Efficiently creating a data frame that is made up of smaller data frames](https://discourse.julialang.org/t/efficiently-creating-a-data-frame-that-is-made-up-of-smaller-data-frames/87055)

<div class="topic-metadata">

**Author:** [@erickawaguchi](https://discourse.julialang.org/u/erickawaguchi)\
**Replies:** 5\
**Last updated:** [September 11, 2022, 10:25am UTC](https://discourse.julialang.org/t/efficiently-creating-a-data-frame-that-is-made-up-of-smaller-data-frames/87055 "2022-09-11T10:25:43Z")

</div>

Hi all, I’m trying to create a “long” version data frame (e.g. several observations per individual as in a longitudinal study). However, the data frame for each individual will depend on certain parameters - not the sam…

---

## [Speedup optimization in nested for loop](https://discourse.julialang.org/t/speedup-optimization-in-nested-for-loop/86880)

<div class="topic-metadata">

**Author:** [@tsojtc](https://discourse.julialang.org/u/tsojtc)\
**Replies:** 1\
**Last updated:** [September 7, 2022, 12:05pm UTC](https://discourse.julialang.org/t/speedup-optimization-in-nested-for-loop/86880 "2022-09-07T12:05:43Z")

</div>

I have adapted some python code that takes data which is G x N, and returns the (strictly upper triangular) matrix cost\_permuted whose (i,j)-th entry represents the minimum of cooscillation\_distance(data\[i, :\], data\[j, :…

---

## [Sum of two vectors of different lengths](https://discourse.julialang.org/t/sum-of-two-vectors-of-different-lengths/86751)

<div class="topic-metadata">

**Author:** [@moataz-sabry](https://discourse.julialang.org/u/moataz-sabry)\
**Replies:** 2\
**Last updated:** [September 4, 2022, 11:50am UTC](https://discourse.julialang.org/t/sum-of-two-vectors-of-different-lengths/86751 "2022-09-04T11:50:39Z")

</div>

I’m looking for a short for loop to sum two vectors of different lengths as in the following MWE. In general y could be longer, shorter than x or equal to it in length. julia\> x = \[1, 2, 3, 4\] 4-element Vector{Int64}: …

---

## [Loop (i, j) without zip() stopped at the 2nd round](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810)

<div class="topic-metadata">

**Author:** [@JiuuJulia](https://discourse.julialang.org/u/JiuuJulia)\
**Replies:** 9\
**Last updated:** [August 17, 2022, 1:33am UTC](https://discourse.julialang.org/t/loop-i-j-without-zip-stopped-at-the-2nd-round/85810 "2022-08-17T01:33:34Z")

</div>

I’m trying to loop two DNA sequences: a = "AAAGCTAGCTAGCTAGACT" b = "ATAGCTAGCCAGCTAAACT" for (i, j) in (a,b) println(i, j) end It gives a result: AA AT Why does the loop stop at the 2nd iteration? I know I’d b…

---

## [For loop : mixing iterator with argument of a function](https://discourse.julialang.org/t/for-loop-mixing-iterator-with-argument-of-a-function/85846)

<div class="topic-metadata">

**Author:** [@Julia\_fun\_123](https://discourse.julialang.org/u/Julia_fun_123)\
**Replies:** 4\
**Last updated:** [August 16, 2022, 9:56pm UTC](https://discourse.julialang.org/t/for-loop-mixing-iterator-with-argument-of-a-function/85846 "2022-08-16T21:56:30Z")

</div>

Hello everyone. I am using Julia 1.7.3 with Jupyter Lab, and I have this code (which is partially taken from this link : Converting image colors · ColorSchemeTools) : using FileIO, ColorSchemes, ColorSchemeTools, Imag…

---

## [Weird behaviour of variable scope in Julia Loops](https://discourse.julialang.org/t/weird-behaviour-of-variable-scope-in-julia-loops/84359)

<div class="topic-metadata">

**Author:** [@shwetank\_verma](https://discourse.julialang.org/u/shwetank_verma)\
**Replies:** 1\
**Last updated:** [July 17, 2022, 3:49pm UTC](https://discourse.julialang.org/t/weird-behaviour-of-variable-scope-in-julia-loops/84359 "2022-07-17T15:49:33Z")

</div>

Hey everyone, I have noticed this peculiar behavior of variable scope inside the for loops in Julia. I am calling it peculiar as I have not seen this in Python. Consider this Python code: for i in range(1,10): if …

---

## [Code speed decreases when loop number increases](https://discourse.julialang.org/t/code-speed-decreases-when-loop-number-increases/78920)

<div class="topic-metadata">

**Author:** [@jisutich](https://discourse.julialang.org/u/jisutich)\
**Replies:** 18\
**Last updated:** [April 3, 2022, 7:11pm UTC](https://discourse.julialang.org/t/code-speed-decreases-when-loop-number-increases/78920 "2022-04-03T19:11:50Z")

</div>

Hi I am using a small code to generate some results. Now I want to take the average of different realizations. So I simply add a more for loop with loop number Nr. But I find that when I increase Nr, the code speed beco…

[Next page](https://discourse.julialang.org/tag/for-loop/909.md?match_all_tags=true&page=1&tags%5B%5D=for-loop)
