# \#iterators

**URL:** https://discourse.julialang.org/tag/iterators/579.md?page=1

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

**Page:** 2

---

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

---

## [Chunk an array](https://discourse.julialang.org/t/chunk-an-array/101878)

<div class="topic-metadata">

**Author:** [@tommy\_J](https://discourse.julialang.org/u/tommy_J)\
**Replies:** 5\
**Last updated:** [July 21, 2023, 3:12pm UTC](https://discourse.julialang.org/t/chunk-an-array/101878 "2023-07-21T15:12:02Z")

</div>

Hello to all, I would like to know how the most efficient way to chunk a multi-dimensional array in julia. arr = rand(Float64, (10, 100)) How can split it into chunks of size 30 so that I get 3 (10, 30) and 1 (10,10) …

---

## [Iterator from 3 vectors](https://discourse.julialang.org/t/iterator-from-3-vectors/101751)

<div class="topic-metadata">

**Author:** [@Yuri\_Masakazu\_Mizusa](https://discourse.julialang.org/u/Yuri_Masakazu_Mizusa)\
**Replies:** 5\
**Last updated:** [July 18, 2023, 9:38pm UTC](https://discourse.julialang.org/t/iterator-from-3-vectors/101751 "2023-07-18T21:38:02Z")

</div>

Hi, I’m trying to create an iterator that does the following: vec\_coord\_x=\[1,2,3\] vec\_coord\_y=\[1,2,3\] vec\_coord\_z=\[1,2,3\] \[(x,y,z) for x in vec\_coord\_x for y in vec\_coord\_y for z in vec\_coord\_z\] Resulting in: 27-ele…

---

## [Base.Iterators.partition for Dict and Set](https://discourse.julialang.org/t/base-iterators-partition-for-dict-and-set/101414)

<div class="topic-metadata">

**Author:** [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Replies:** 0\
**Last updated:** [July 9, 2023, 11:53pm UTC](https://discourse.julialang.org/t/base-iterators-partition-for-dict-and-set/101414 "2023-07-09T23:53:07Z")

</div>

A recent blog post highlighted the usage of Base.Iterators.partition for correctly managing state in a multithreaded code. I checked how this function behaves on Dict and Set and found that it copies the data (see trace…

---

## [Custom Iterator with one big computation at the begin + in place operation at every iterations](https://discourse.julialang.org/t/custom-iterator-with-one-big-computation-at-the-begin-in-place-operation-at-every-iterations/100045)

<div class="topic-metadata">

**Author:** [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Replies:** 2\
**Last updated:** [June 13, 2023, 11:44am UTC](https://discourse.julialang.org/t/custom-iterator-with-one-big-computation-at-the-begin-in-place-operation-at-every-iterations/100045 "2023-06-13T11:44:15Z")

</div>

I am trying to build a custom iterator for the QuasiMonteCarlo.jl package. I did not find an example doing the kind of things I want. I need first to generate 3 big matrices for 1:M “randomize” in place these matr…

---

## [Transducers vs Iterators](https://discourse.julialang.org/t/transducers-vs-iterators/98771)

<div class="topic-metadata">

**Author:** [@jekyllstein](https://discourse.julialang.org/u/jekyllstein)\
**Replies:** 0\
**Last updated:** [May 12, 2023, 11:03pm UTC](https://discourse.julialang.org/t/transducers-vs-iterators/98771 "2023-05-12T23:03:42Z")

</div>

I have an example of a simple processing pipeline where I noticed Transducers.jl was making a lot of allocations and behaving slowly compared to compositing iterators and generators together. I’m wondering if there’s a …

---

## [Filtering elements of a generator without collecting](https://discourse.julialang.org/t/filtering-elements-of-a-generator-without-collecting/96664)

<div class="topic-metadata">

**Author:** [@cgois](https://discourse.julialang.org/u/cgois)\
**Replies:** 5\
**Last updated:** [March 27, 2023, 8:21pm UTC](https://discourse.julialang.org/t/filtering-elements-of-a-generator-without-collecting/96664 "2023-03-27T20:21:56Z")

</div>

Hi everyone, I am playing around with iterators and I have the following problem. Lets us say we want to compute using Random weights = shuffle(vcat(1:5, zeros(5))); mats = (randn(10, 10) for \_ in 1:10) # Or any genera…

---

## [Difference between calling Iterators.rest on an array vs a range](https://discourse.julialang.org/t/difference-between-calling-iterators-rest-on-an-array-vs-a-range/96533)

<div class="topic-metadata">

**Author:** [@phantom](https://discourse.julialang.org/u/phantom)\
**Replies:** 3\
**Last updated:** [March 24, 2023, 5:33pm UTC](https://discourse.julialang.org/t/difference-between-calling-iterators-rest-on-an-array-vs-a-range/96533 "2023-03-24T17:33:03Z")

</div>

I’m just curious why Iterators.rest yields different results depending on whether a range or an array is used. eg df1 = DataFrame( :x=\> 1:5) To start a loop on the second iteration of a range I would use collect(I…

---

## [IndexStyle for pairs iterator?](https://discourse.julialang.org/t/indexstyle-for-pairs-iterator/96450)

<div class="topic-metadata">

**Author:** [@DNF](https://discourse.julialang.org/u/DNF)\
**Replies:** 0\
**Last updated:** [March 22, 2023, 3:06pm UTC](https://discourse.julialang.org/t/indexstyle-for-pairs-iterator/96450 "2023-03-22T15:06:40Z")

</div>

I was a bit surprised to see that the pairs iterator for Matrix returns CartesianIndices by default. The docstring of pairs includes a section on specifying index style, showing these three signatures: pairs(IndexLinear…

---

## [Group by criteria](https://discourse.julialang.org/t/group-by-criteria/96246)

<div class="topic-metadata">

**Author:** [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Replies:** 17\
**Last updated:** [March 19, 2023, 8:10am UTC](https://discourse.julialang.org/t/group-by-criteria/96246 "2023-03-19T08:10:38Z")

</div>

How would you solve the following problem? Given the following array of strings we want to find all groups whose group leader starts with “AT”. All the elements that are found before the first one starting with “AT” fo…

---

## [How to access partitions used in previous iterations of a loop over a partitioned set?](https://discourse.julialang.org/t/how-to-access-partitions-used-in-previous-iterations-of-a-loop-over-a-partitioned-set/96127)

<div class="topic-metadata">

**Author:** [@phantom](https://discourse.julialang.org/u/phantom)\
**Replies:** 10\
**Last updated:** [March 16, 2023, 11:41am UTC](https://discourse.julialang.org/t/how-to-access-partitions-used-in-previous-iterations-of-a-loop-over-a-partitioned-set/96127 "2023-03-16T11:41:24Z")

</div>

Suppose I am looping over a partitioned set. I would like to check a condition or compare a value in the current partition to that of a partition used in a previous iteration or to be used in a future iteration. What wo…

---

## [Is it always safe to “re-use” an iterator/generator?](https://discourse.julialang.org/t/is-it-always-safe-to-re-use-an-iterator-generator/95697)

<div class="topic-metadata">

**Author:** [@lmtzx9h4qqnt](https://discourse.julialang.org/u/lmtzx9h4qqnt)\
**Replies:** 8\
**Last updated:** [March 15, 2023, 2:21am UTC](https://discourse.julialang.org/t/is-it-always-safe-to-re-use-an-iterator-generator/95697 "2023-03-15T02:21:05Z")

</div>

The way iterators work in Python, there is the following (potentially surprising) behavior: \>\>\> l = \[1, 2, 3\] \>\>\> g = (x for x in l) \>\>\> list(g) \[1, 2, 3\] \>\>\> list(g) \[\] That is, iterators are “used up” during the iter…

---

## [Why is it that dataframe violates iterator interface?](https://discourse.julialang.org/t/why-is-it-that-dataframe-violates-iterator-interface/95906)

<div class="topic-metadata">

**Author:** [@freeman](https://discourse.julialang.org/u/freeman)\
**Replies:** 7\
**Last updated:** [March 13, 2023, 2:18am UTC](https://discourse.julialang.org/t/why-is-it-that-dataframe-violates-iterator-interface/95906 "2023-03-13T02:18:59Z")

</div>

using DataFrames df = DataFrame(:a=\>\[1,2,3\]) Base.IteratorSize(DataFrame) # Base.HasLength() length(df) # method length(::DataFrame) doesn't exist I’m ok with DataFrames not having length, but in that case why Base…

---

## [Equivalent of \`last\` for iterators](https://discourse.julialang.org/t/equivalent-of-last-for-iterators/95874)

<div class="topic-metadata">

**Author:** [@Dan](https://discourse.julialang.org/u/Dan)\
**Replies:** 1\
**Last updated:** [March 10, 2023, 5:13pm UTC](https://discourse.julialang.org/t/equivalent-of-last-for-iterators/95874 "2023-03-10T17:13:05Z")

</div>

While responding to a different topic, I needed the last element of an iterator. This has at least one fairly simple and efficient solution which has appeared elsewhere: itrlast(itr) = foldl((\_,y)-\>y,itr) but last can …

---

## [Stateful iterators with known eltype](https://discourse.julialang.org/t/stateful-iterators-with-known-eltype/93528)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 0\
**Last updated:** [January 25, 2023, 6:46pm UTC](https://discourse.julialang.org/t/stateful-iterators-with-known-eltype/93528 "2023-01-25T18:46:04Z")

</div>

Iterators.Stateful tries to determine the type of the (value, state) tuple using internal introspection tools. This can be complicated if the type of state keeps changing across iterations, and in such cases the inferred…

---

## [How to improve a Generator to be more memory efficient when it is collected?](https://discourse.julialang.org/t/how-to-improve-a-generator-to-be-more-memory-efficient-when-it-is-collected/92932)

<div class="topic-metadata">

**Author:** [@camilogarciabotero](https://discourse.julialang.org/u/camilogarciabotero)\
**Replies:** 21\
**Last updated:** [January 23, 2023, 6:26pm UTC](https://discourse.julialang.org/t/how-to-improve-a-generator-to-be-more-memory-efficient-when-it-is-collected/92932 "2023-01-23T18:26:18Z")

</div>

Hi all, I wrote a function that searches through a sequence (a BioSequences.LongDNA but could also be a String) for start codons (predefined as constants subsequence searches) and generates ranges of indices correspondi…

---

## [Why does stateful iterators restrict length to \`Signed\`?](https://discourse.julialang.org/t/why-does-stateful-iterators-restrict-length-to-signed/93263)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 2\
**Last updated:** [January 20, 2023, 2:56pm UTC](https://discourse.julialang.org/t/why-does-stateful-iterators-restrict-length-to-signed/93263 "2023-01-20T14:56:53Z")

</div>

Iterators.Stateful has the definition mutable struct Stateful{T, VS, N\<:Integer} itr::T # A bit awkward right now, but adapted to the new iteration protocol nextvalstate::Union{VS, Nothing} # Number of …

---

## [Can \`findmin\` use a counter if \`keys\` is not defined for an iterator?](https://discourse.julialang.org/t/can-findmin-use-a-counter-if-keys-is-not-defined-for-an-iterator/93069)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 12\
**Last updated:** [January 19, 2023, 7:45am UTC](https://discourse.julialang.org/t/can-findmin-use-a-counter-if-keys-is-not-defined-for-an-iterator/93069 "2023-01-19T07:45:30Z")

</div>

I want the index for which an iterator itr is minimized. I had expected \_, ind = findmin(f, itr) to return the index, but this doesn’t work if keys(itr) is not defined. What does work is: ind, \_ = argmin(((ind, v),) -\>…

---

## [Obtaining the type of the state object returned by Iterate](https://discourse.julialang.org/t/obtaining-the-type-of-the-state-object-returned-by-iterate/59190)

<div class="topic-metadata">

**Author:** [@Olof\_Salberger](https://discourse.julialang.org/u/Olof_Salberger)\
**Replies:** 4\
**Last updated:** [December 17, 2022, 12:15pm UTC](https://discourse.julialang.org/t/obtaining-the-type-of-the-state-object-returned-by-iterate/59190 "2022-12-17T12:15:35Z")

</div>

An issue I’ve run into is that if you have a container type T, in general it isn’t possible to obtain the type of state returned by iterate in the same way as you can get eltype(T) since there is no (let’s call it iterat…

---

## [Unintuitive behavior for iterator of views of a vector](https://discourse.julialang.org/t/unintuitive-behavior-for-iterator-of-views-of-a-vector/90334)

<div class="topic-metadata">

**Author:** [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Replies:** 1\
**Last updated:** [November 16, 2022, 10:00am UTC](https://discourse.julialang.org/t/unintuitive-behavior-for-iterator-of-views-of-a-vector/90334 "2022-11-16T10:00:57Z")

</div>

We are creating a simple iterator whose goal is to generate views of a given vector (something like a sliding window). However, the Julia behavior on this object is rather unituitive to me, with weird type instabilities …

---

## [The default iteration interface can cause memory leak?](https://discourse.julialang.org/t/the-default-iteration-interface-can-cause-memory-leak/88986)

<div class="topic-metadata">

**Author:** [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Replies:** 5\
**Last updated:** [October 20, 2022, 10:55pm UTC](https://discourse.julialang.org/t/the-default-iteration-interface-can-cause-memory-leak/88986 "2022-10-20T22:55:39Z")

</div>

I just read about the post “Iterate on it” which says the current implementation of iterate could cause a memory leak since the xs must remain alive over the entire loop and that is uneconomic for linked lists: next = i…

---

## [Grid of all elements of an arbitrarily sized matrix](https://discourse.julialang.org/t/grid-of-all-elements-of-an-arbitrarily-sized-matrix/89024)

<div class="topic-metadata">

**Author:** [@CeterisPartybus](https://discourse.julialang.org/u/CeterisPartybus)\
**Replies:** 7\
**Last updated:** [October 20, 2022, 5:35pm UTC](https://discourse.julialang.org/t/grid-of-all-elements-of-an-arbitrarily-sized-matrix/89024 "2022-10-20T17:35:17Z")

</div>

I have an n\*m sized array A, and I would like to create a grid of its elements. The output should look something like: B = (A\[1,1\], A\[2,1\] A\[1,1\], A\[2,2\] ... A\[1,1\], A\[2,m\] A\[1,2\], A\[2,1\] A\[1,2\]…

---

## [Construct Big Integers with odd digits only](https://discourse.julialang.org/t/construct-big-integers-with-odd-digits-only/86927)

<div class="topic-metadata">

**Author:** [@blackeneth](https://discourse.julialang.org/u/blackeneth)\
**Replies:** 5\
**Last updated:** [September 8, 2022, 10:29am UTC](https://discourse.julialang.org/t/construct-big-integers-with-odd-digits-only/86927 "2022-09-08T10:29:08Z")

</div>

My question is essentially: what is the best way to solve this problem with Julia? I don’t think I have the “right words” or “concepts” to even research a solution. The Problem: Say I wanted to construct positive inte…

---

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

---

## [Inverting default \`collect\` behavior when iterator returns an array](https://discourse.julialang.org/t/inverting-default-collect-behavior-when-iterator-returns-an-array/85409)

<div class="topic-metadata">

**Author:** [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Replies:** 3\
**Last updated:** [August 7, 2022, 3:56am UTC](https://discourse.julialang.org/t/inverting-default-collect-behavior-when-iterator-returns-an-array/85409 "2022-08-07T03:56:54Z")

</div>

A MWE: struct MyIter n limit end function Base.iterate(m::MyIter) state = ones(m.n) return (state,state .+ 1) end function Base.iterate(m::MyIter,state) if first(state) \> m.limit return no…

---

## [RGB has a length but no iterate?](https://discourse.julialang.org/t/rgb-has-a-length-but-no-iterate/84446)

<div class="topic-metadata">

**Author:** [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Replies:** 2\
**Last updated:** [July 19, 2022, 9:34am UTC](https://discourse.julialang.org/t/rgb-has-a-length-but-no-iterate/84446 "2022-07-19T09:34:36Z")

</div>

I’m defining a handy function for me to flatten data recursively into matrices and tensors. One way to do it is to descend into the list-of-lists until we see something with length(x)==1, and then we consider that to be …

---

## [Improving the performance of a sum over a vector of vectors](https://discourse.julialang.org/t/improving-the-performance-of-a-sum-over-a-vector-of-vectors/84150)

<div class="topic-metadata">

**Author:** [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Replies:** 10\
**Last updated:** [July 13, 2022, 10:36pm UTC](https://discourse.julialang.org/t/improving-the-performance-of-a-sum-over-a-vector-of-vectors/84150 "2022-07-13T22:36:19Z")

</div>

I want to consider the sum over a vector of vectors. Consider the following benchmarks for a problem involving the sum over some data (getting a log-likelihood): using BenchmarkTools, Random, LoopVectorization Random.se…

---

## [How to create all possible k-mers with a set of alphabets?](https://discourse.julialang.org/t/how-to-create-all-possible-k-mers-with-a-set-of-alphabets/83186)

<div class="topic-metadata">

**Author:** [@Bharat\_Ravi](https://discourse.julialang.org/u/Bharat_Ravi)\
**Replies:** 12\
**Last updated:** [June 29, 2022, 4:07pm UTC](https://discourse.julialang.org/t/how-to-create-all-possible-k-mers-with-a-set-of-alphabets/83186 "2022-06-29T16:07:03Z")

</div>

I am trying to use Iterators.product to generate a list of all possible k-mers with a set of alphabets. Since I am a biologist, these letters happen to be the bases of DNA :smiley: : “ATGC” If I want to generate 3-me…

---

## [Iterator flatten with nested tuples](https://discourse.julialang.org/t/iterator-flatten-with-nested-tuples/83339)

<div class="topic-metadata">

**Author:** [@jinml](https://discourse.julialang.org/u/jinml)\
**Replies:** 3\
**Last updated:** [June 25, 2022, 8:17pm UTC](https://discourse.julialang.org/t/iterator-flatten-with-nested-tuples/83339 "2022-06-25T20:17:43Z")

</div>

I am trying to flatten the following Iterator: julia\> it=Iterators.product(1:3,1:4); collect(it) 3×4 Matrix{Tuple{Int64, Int64}}: (1, 1) (1, 2) (1, 3) (1, 4) (2, 1) (2, 2) (2, 3) (2, 4) (3, 1) (3, 2) (3, 3) …

---

## [Product iterator with NamedTuples](https://discourse.julialang.org/t/product-iterator-with-namedtuples/83319)

<div class="topic-metadata">

**Author:** [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Replies:** 5\
**Last updated:** [June 25, 2022, 5:42pm UTC](https://discourse.julialang.org/t/product-iterator-with-namedtuples/83319 "2022-06-25T17:42:29Z")

</div>

Hi all, I was wondering whether it is possible to create a product iterator consisting of NamedTuples. The product iterator generates tuples like so: Base.product(1:3,4:5) |\> collect 3×2 Matrix{Tuple{Int64, Int64}}: …

[Previous page](https://discourse.julialang.org/tag/iterators/579.md?match_all_tags=true&tags%5B%5D=iterators)

[Next page](https://discourse.julialang.org/tag/iterators/579.md?match_all_tags=true&page=2&tags%5B%5D=iterators)
