# Creating histos trough for loop (PyROOT)

**URL:** <https://root-forum.cern.ch/t/creating-histos-trough-for-loop-pyroot/40217>\
**Category:** Newbie\
**Created:** [July 2, 2020, 9:11pm UTC](https://root-forum.cern.ch/t/creating-histos-trough-for-loop-pyroot/40217 "2020-07-02T21:11:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jailbone](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@Jailbone](https://root-forum.cern.ch/u/Jailbone)\
**Post date:** [July 2, 2020, 9:11pm UTC](https://root-forum.cern.ch/t/creating-histos-trough-for-loop-pyroot/40217/1 "2020-07-02T21:11:30Z")

</div>

I am trying to get a feel for pyRoot and have a basic beginner question:  
How do I create Histograms trough a loop?

I’ve got this code which should create two identical histograms and save them to a root file. I know the code is wrong and it does not quite make sense to me neither but you should get the idea of what I am trying to do:

```auto
import ROOT as root
from ROOT import gROOT, TCanvas, TF1, TH1, TFile

f = root.TFile("demo.root", "RECREATE")

c1 = root.TCanvas()

for i in range(2):

	h[i] = root.TH1D("h" + str(i), "Histogram" + str(i), 10, 0, 10)

	for _ in range(1000):
		g = root.gRandom.Gaus(5,2)
		h[i].Fill(g)
		

	h[i].Draw()

c1.Update()

f.Write()

```

Appreciate the help.

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [July 3, 2020, 8:29am UTC](https://root-forum.cern.ch/t/creating-histos-trough-for-loop-pyroot/40217/2 "2020-07-03T08:29:02Z")

</div>

Hello,

In order to make the code work, try to write `h = []` before the `for` loop. Then, instead of

```
h[i] = ...

```

use

```
h.append(...)

```

Cheers,  
Jakob
