# Array of tuples into a pure array of integers

**URL:** <https://discourse.julialang.org/t/array-of-tuples-into-a-pure-array-of-integers/46811>\
**Category:** New to Julia\
**Created:** [September 17, 2020, 11:43pm UTC](https://discourse.julialang.org/t/array-of-tuples-into-a-pure-array-of-integers/46811 "2020-09-17T23:43:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dale\_James\_Black](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dale_james_black/32/11595_2.png) [@Dale\_James\_Black](https://discourse.julialang.org/u/Dale_James_Black)\
**Post date:** [September 17, 2020, 11:43pm UTC](https://discourse.julialang.org/t/array-of-tuples-into-a-pure-array-of-integers/46811/1 "2020-09-17T23:43:01Z")

</div>

New to Julia from Python so I am not used to all of these types, sorry if this is super simple but I have had no luck in my google searches.

Can someone help me turn an array of tuples into a pure array of integers.

For example:

```julia
cartesian_array = [(1,2) (2,2) (3,2)]
new_array = [1 2; 2 2; 3 2]

```

---

<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:** [September 18, 2020, 12:00am UTC](https://discourse.julialang.org/t/array-of-tuples-into-a-pure-array-of-integers/46811/2 "2020-09-18T00:00:52Z")

</div>

just to post root cause here:  
[https://github.com/JuliaStats/Distances.jl/issues/177](https://github.com/JuliaStats/Distances.jl/issues/177)

so basically the question was how to convert `CartesianIndex` to `Array` most efficiently (since `Distances.jl` doesn’t like `Tuple` either)

---

<div class="post-metadata">

**Author:** ![zachmatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zachmatson/32/9397_2.png) [@zachmatson](https://discourse.julialang.org/u/zachmatson)\
**Post date:** [September 18, 2020, 12:22am UTC](https://discourse.julialang.org/t/array-of-tuples-into-a-pure-array-of-integers/46811/3 "2020-09-18T00:22:14Z")

</div>

Glad to see you are giving Julia a look! It’s a great language to use.

In Julia, `[1 2; 2 2; 3 2]` means a 2D matrix where spaces separate entries in a row, and semicolons separate different rows. Are you looking for this or a flat array?

Not sure if these are the best ways but if you needed some simple conversions:  
For flat array: `[(cartesian_array...)...]` [credit](https://stackoverflow.com/a/61797748/11396735)  
For a matrix: `[x[i] for x in [cartesian_array...], i in 1:2]`

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [September 18, 2020, 5:02am UTC](https://discourse.julialang.org/t/array-of-tuples-into-a-pure-array-of-integers/46811/4 "2020-09-18T05:02:49Z")

</div>

For a significantly faster approach, you can use `reinterpret` and `reshape` to do the conversion without even having to copy the data:

```julia
julia> cartesian_array = [(1,2) (2,2) (3,2)]
1×3 Array{Tuple{Int64,Int64},2}:
 (1, 2) (2, 2) (3, 2)

julia> reshape(reinterpret(Int, cartesian_array), (2, :))
2×3 reshape(reinterpret(Int64, ::Array{Tuple{Int64,Int64},2}), 2, 3) with eltype Int64:
 1 2 3
 2 2 2

```

`reinterpret` creates a lazy view into an existing array, interpreting it as a different data type. In this case, we are interpreting a list of tuples as a flat vector of integers. `reshape` then creates another view of that same data with a different shape, in this case a 2xN matrix.

Note that this produces a 2xN matrix instead of an Nx2 matrix. That’s almost always what you want in Julia, since Julia is column major (this is the opposite of C, C++, and NumPy which are row-major). If it’s not what you want, you can call `transpose` to get the Nx2 version instead (`transpose` is lazy too, so this doesn’t copy any of the data).

Comparing performance:

```julia
julia> N = 100
100

julia> cartesian_array = [(rand(Int), rand(Int)) for _ in 1:N];

julia> function f1(c)
         [x[i] for x in [cartesian_array...], i in 1:2]
       end
f1 (generic function with 1 method)

julia> function f2(c)
         transpose(reshape(reinterpret(Int, c), (2, :)))
       end
f2 (generic function with 1 method)

julia> using BenchmarkTools

julia> @btime f1($cartesian_array);
  2.450 μs (107 allocations: 6.81 KiB)

julia> @btime f2($cartesian_array);
  10.288 ns (0 allocations: 0 bytes)

```

Using `reshape` and `reinterpret` is about 240 times faster than the list comprehension approach for a 100-element array.
