# "Initializer list" for std.vector

**URL:** <https://root-forum.cern.ch/t/initializer-list-for-std-vector/18854>\
**Category:** ROOT\
**Created:** [February 18, 2015, 1:12am UTC](https://root-forum.cern.ch/t/initializer-list-for-std-vector/18854 "2015-02-18T01:12:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [February 18, 2015, 1:12am UTC](https://root-forum.cern.ch/t/initializer-list-for-std-vector/18854/1 "2015-02-18T01:12:56Z")

</div>

Hi, I’m wondering if it’s possible to initialize a ROOT.std.vector(ROOT.Double) with values from a Python collection in one line.

Currently, one has to do something like this:

```auto
v = ROOT.std.vector(ROOT.Double)()
[v.push_back(x) for x in [5,6,7,8]]
```

Is there a way to do what I mean here?:

```auto
v = ROOT.std.vector(ROOT.Double)([5,6,7,8])
```

Even if it’s a workaround with array.array(‘d’,[5,6,7,8]) or something, it’d be nice to be able to create std.vectors on-the-fly and as temporaries in function calls.

Jean-François

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [February 20, 2015, 7:09pm UTC](https://root-forum.cern.ch/t/initializer-list-for-std-vector/18854/2 "2015-02-20T19:09:47Z")

</div>

Hi,

I have asked Wim for help…

Axel.

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [February 20, 2015, 11:28pm UTC](https://root-forum.cern.ch/t/initializer-list-for-std-vector/18854/3 "2015-02-20T23:28:03Z")

</div>

Hi,

of course it’s possible. 🙂 You can replace **init** of vector, after all, to do the push\_back. Note that similar is already done for +=. This works:[code]\>\>\> from ROOT import std

> > > v = std.vector(‘float’)()  
> > > v += [4,5,6,7]  
> > > print list(v)  
> > > [4.0, 5.0, 6.0, 7.0]  
> > > [/code]  
> > > The more general question of initializer list is something that I haven’t looked into yet. It will depend on what the representation on the Cling side is. Looks like (from the **init** doc string):` vector<float>::vector<float>(initializer_list<vector<float>::value_type> __l, const vector<float>::allocator_type&__ a)`so looks doable if I can actually create an object of type initializer\_list.

Actually, continuing this thread of thought, I find why I love cling soo much:[code]\>\>\> help(std.initializer\_list(‘float’). **init** )  
Help on method **init** in module ROOT:

**init** (self, \*args) unbound cppyy.initializer\_list method  
initializer\_list::initializer\_list()  
initializer\_list::initializer\_list(const initializer\_list&)  
initializer\_list::initializer\_list(initializer\_list&&)[/code]Ah, just like that. 🙂

But iow., no: in fact, the only useful constructor of an initializer\_list is private and can only be called by the compiler, so the ticket will likely be to generate a C++ helper.

Will look into that once I have time again.

Aside, it’s a long-standing TODO to allow Python list where std::vector is expected for (temporary) function arguments.

Cheers,  
Wim

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [February 21, 2015, 7:55am UTC](https://root-forum.cern.ch/t/initializer-list-for-std-vector/18854/4 "2015-02-21T07:55:20Z")

</div>

Strange that you can do

```auto
v = ROOT.std.vector(ROOT.Double)()
v += [5,6,7,8]
```

but not

```auto
v = ROOT.std.vector(ROOT.Double)() + [5,6,7,8]
```

When you say replace the **init** method of vector, you mean like monkey-patching on a new **init** function? I never thought of doing that.

I’m glad stuff is in the works. If it proves difficult to allow python lists where ROOT expects vectors or arrays, it’s probably easier to use python array.array objects, since those are fixed-type and contiguous in memory, afaik.

Jean-François

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [February 22, 2015, 12:51am UTC](https://root-forum.cern.ch/t/initializer-list-for-std-vector/18854/5 "2015-02-22T00:51:56Z")

</div>

Jean-François,

is b/c only **iadd** has been added to take a list, not **add**. And yes, I meant monkey-patching. Is no different then for **iadd** , see in [cppyy.py](http://cppyy.py):[code] result = \_backend.MakeRootTemplateClass( \*newargs )

```
   # special case pythonization (builtin_map is not available from the C-API)
     if hasattr( result, 'push_back' ):
        def iadd( self, ll ):
           [self.push_back(x) for x in ll]
           return self

        result. __iadd__ = iadd[/code]

```

array.array need not be contiguous (but that can be checked at run-time). Even so, it matters not: values still need to be copied over into the temporary std::vector. What is saved, of course, is the unpacking of PyObjects (and their checking).

Cheers,  
Wim
