# Base.Iterators.partition for Dict and Set

**URL:** <https://discourse.julialang.org/t/base-iterators-partition-for-dict-and-set/101414>\
**Category:** Internals & Design\
**Tags:** iterators\
**Created:** [July 9, 2023, 11:53pm UTC](https://discourse.julialang.org/t/base-iterators-partition-for-dict-and-set/101414 "2023-07-09T23:53:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [July 9, 2023, 11:53pm UTC](https://discourse.julialang.org/t/base-iterators-partition-for-dict-and-set/101414/1 "2023-07-09T23:53:07Z")

</div>

A recent [blog post](https://julialang.org/blog/2023/07/PSA-dont-use-threadid/) 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 below) instead of creating a lazy structure that can iterate directly over the entries of the dictionary or set. I suppose that a lazy structure would be more performant for most applications. In the future, I may also implement efficient `Base.Iterators.partition` methods for SortedDict, SortedSet, and SortedMultiSet in DataStructures.jl. Therefore, I am wondering:

1. Why does Base copy the data for this operation on Dict and Set?

2. Would it be a breaking change to reimplement `Base.Iterators.partition` lazily for Dict and Set instead of copying? For most usages, the change would be invisible, but in some odd cases like changing the data structure while iterating over it, this change could break a user’s code.

```julia
julia> s = Set(1:9);

julia> u = Base.Iterators.partition(s,4);

julia> for i in u
       println(i, " ", typeof(i))
       end
[5, 4, 6, 7] Vector{Int64}
[2, 9, 8, 3] Vector{Int64}
[1] Vector{Int64}

```
