# Iterating array like numpy?

**URL:** <https://discourse.julialang.org/t/iterating-array-like-numpy/15059>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 17, 2018, 11:39am UTC](https://discourse.julialang.org/t/iterating-array-like-numpy/15059 "2018-09-17T11:39:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiang0x48](https://avatars.discourse-cdn.com/v4/letter/x/53a042/32.png) [@xiang0x48](https://discourse.julialang.org/u/xiang0x48)\
**Post date:** [September 17, 2018, 11:39am UTC](https://discourse.julialang.org/t/iterating-array-like-numpy/15059/1 "2018-09-17T11:39:52Z")

</div>

Julia seems broadcast/iterate array by elements:

```julia
arr = zeros(Float32, 3, 2)
for x in arr
println(x)
end
# output 6 lines of 0.0

```

I’m wondering if we can iterate normal arrays like numpy fashion, thus can we have:

```julia
arr = zeros(Float32, 3, 2)
for x in arr # or something others
println(x)
end
# output 3 lines of (0.0, 0.0)

```

I noticed that

```julia
arr = [[1,2],[3,4]]
for x in arr
println(x)
end

```

will produce

```julia
[1,2]
[3,4]

```

but type of `arr` here is `Array{Array{Int64,1},1}`, not `Array{Int64, 2}`, and can not select element by `arr[1,2]`.

As an summary, can we broadcast only on first dimension(or a specified dimension/axis) of an array?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 17, 2018, 12:05pm UTC](https://discourse.julialang.org/t/iterating-array-like-numpy/15059/2 "2018-09-17T12:05:29Z")

</div>

Does this help?

```julia
julia> A = rand(1:10, 3, 2)
3×2 Array{Int64,2}:
 2 9
 1 8
 3 3

julia> for rowindex in axes(A, 1)
       @show A[rowindex, :]
       end
A[rowindex, :] = [2, 9]
A[rowindex, :] = [1, 8]
A[rowindex, :] = [3, 3]

```

You can use a `@view` to speed things up in some cases (benchmark!).

If you need an actual iterable for the rows (or their views), that is also easy, just ask.
