## itertools to numpy array

Question

I would like to use itertools' various functions to create numpy arrays. I can easily compute ahead of time the number of elements in the product, combinations, permutations, etc, so allotting space shouldn't be a problemo.

e.g.

```
coords = [[1,2,3],[4,5,6]]
iterable = itertools.product(*coords)
shape = (len(coords[0]), len(coords[1]))
arr = np.iterable_to_array(
iterable,
shape=shape,
dtype=np.float64,
count=shape[0]*shape[1]
) #not a real thing
answer = np.array([
[1,4],[1,5],[1,6],
[2,4],[2,5],[2,6],
[3,4],[3,5],[3,6]])
assert np.equal(arr, answer)
```

Show source

## Answers ( 1 )

Here are several numpy ways of generating an array with these values

`fromiter`

will work with an appropriate structured`dtype`

:But usually we use the tools that

`numpy`

provides for generating sequences and meshes.`np.arange`

is used all over the place.`meshgrid`

is widely used. With a bit of trial and error I found that I could transpose its output, and produce the same sequence:`repeat`

and`tile`

also useful for tasks like this:I've done some timings on

`fromiter`

in the past. My memory is that it offers only a modest time savings over`np.array`

.A while back I explored

`itertools`

and`fromiter`

, and found a way to combine them using`itertools.chain`

convert itertools array into numpy array