# How to iterate through a StatsBase.Histogram

**URL:** <https://discourse.julialang.org/t/how-to-iterate-through-a-statsbase-histogram/35335>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 29, 2020, 8:35pm UTC](https://discourse.julialang.org/t/how-to-iterate-through-a-statsbase-histogram/35335 "2020-02-29T20:35:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zierenberg](https://avatars.discourse-cdn.com/v4/letter/z/278dde/32.png) [@zierenberg](https://discourse.julialang.org/u/zierenberg)\
**Post date:** [February 29, 2020, 8:35pm UTC](https://discourse.julialang.org/t/how-to-iterate-through-a-statsbase-histogram/35335/1 "2020-02-29T20:35:36Z")

</div>

Hi,  
let me start my first post by saying thank you for all the hard work from the community. I really enjoy diving into the julia programming language.

I am currently playing around with Monte Carlo simulations in julia, right now I am implementing reweighting techniques in particular. For this I need to handle histograms. After writing my first histograms as dictionaries (of course working with discrete states), I found `StatsBase.Histograms` to be quite neat for my cause. One feature that I do not get implemented nicely, however, is to iterate over the whole histogram.

For example, with a dictionary I can do the following

```julia
for (arg, value) in Dict
...
end

```

However, to get this done for StatsBase.Histograms, I am currently doing

```julia
for (i, arg) in enumerate(hist.edges[1])
  value = hist.weights[i]
  ...
end

```

How can I nicely iterate over such histograms?  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 29, 2020, 8:37pm UTC](https://discourse.julialang.org/t/how-to-iterate-through-a-statsbase-histogram/35335/2 "2020-02-29T20:37:51Z")

</div>

well,

```julia
for (edge, weight) in zip(hist.edges[1], hist.weights)
    @show edge, weight
end

```

---

<div class="post-metadata">

**Author:** ![zierenberg](https://avatars.discourse-cdn.com/v4/letter/z/278dde/32.png) [@zierenberg](https://discourse.julialang.org/u/zierenberg)\
**Post date:** [February 29, 2020, 8:46pm UTC](https://discourse.julialang.org/t/how-to-iterate-through-a-statsbase-histogram/35335/3 "2020-02-29T20:46:36Z")

</div>

Thanks jiing,

I was not familiar with the zip() function. Does this bring performance overhead?

Also, I was wondering if there is a way to do this along all dimensions? Let’s imagine I do

```julia
h = fit(Histogram, (rand(100),rand(100)))

```

And let’s further imagine I do not know the dimensionality of the histogram a priori (because it is passed to the function). I was expecting something similar to iterating with CartesianIndices over a Matrix. Does something like this exist?

Thanks

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 1, 2020, 1:21am UTC](https://discourse.julialang.org/t/how-to-iterate-through-a-statsbase-histogram/35335/4 "2020-03-01T01:21:34Z")

</div>

`zip` shouldn’t hinder the performance

* * *

for start, `CartesianIndices(hist.weights)` can give you a way to iterate over weights, and the cartesian index is your bin, if you want to convert the bin number back to edge value, you just need to access the corresponding element in `hist.edges`
