# Preallocate an array of tuples

**URL:** https://discourse.julialang.org/t/preallocate-an-array-of-tuples/54699
**Category:** General Usage
**Tags:** question
**Created:** [February 5, 2021, 5:34pm UTC](https://discourse.julialang.org/t/preallocate-an-array-of-tuples/54699 "2021-02-05T17:34:44Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![nelsonah](https://avatars.discourse-cdn.com/v4/letter/n/7993a0/32.png) [@nelsonah](https://discourse.julialang.org/u/nelsonah)
#### Post date: [February 5, 2021, 5:34pm UTC](https://discourse.julialang.org/t/preallocate-an-array-of-tuples/54699/1 "2021-02-05T17:34:44Z")

</div>

This should be trivial but I’m stumped. Simple code snippet:  
# points = Array{(UInt8, UInt8, UInt8)}(undef, x\_size, y\_size)  
points = Array{undef,(6, 4)} # compiles, but not valid  
val = (100, 225, 255)  
points[2][3] = val # Fails on this line  
Thanks

---

<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: [February 5, 2021, 5:36pm UTC](https://discourse.julialang.org/t/preallocate-an-array-of-tuples/54699/2 "2021-02-05T17:36:49Z")

</div>

```julia
julia> val = (100, 225, 255)
(100, 225, 255)

julia> T = typeof(val) #use this info to determine type of array element
Tuple{Int64, Int64, Int64}

julia> points = Array{T, 2}(undef, 3,3)
3×3 Matrix{Tuple{Int64, Int64, Int64}}:
 (0, 0, 0) (0, 0, 0) (0, 0, 0)
 (0, 0, 0) (0, 0, 0) (0, 0, 0)
 (0, 0, 0) (0, 0, 0) (0, 0, 0)

julia> points[2,3] = val
(100, 225, 255)

julia> points
3×3 Matrix{Tuple{Int64, Int64, Int64}}:
 (0, 0, 0) (0, 0, 0) (0, 0, 0)
 (0, 0, 0) (0, 0, 0) (100, 225, 255)
 (0, 0, 0) (0, 0, 0) (0, 0, 0)

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [February 5, 2021, 5:48pm UTC](https://discourse.julialang.org/t/preallocate-an-array-of-tuples/54699/3 "2021-02-05T17:48:13Z")

</div>

```julia
Matrix{NTuple{3, UInt8}}(undef, xsize, ysize) 

```

`(UInt8, UInt8, UInt8)` isn’t a tuple type, but a tuple of types.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 5, 2021, 6:15pm UTC](https://discourse.julialang.org/t/preallocate-an-array-of-tuples/54699/4 "2021-02-05T18:15:53Z")

</div>

Alternatively,

```julia
Array{Tuple{UInt8, UInt8, UInt8}}(undef, x_size, y_size)

```

---

<div class="post-metadata">

### Author: ![nelsonah](https://avatars.discourse-cdn.com/v4/letter/n/7993a0/32.png) [@nelsonah](https://discourse.julialang.org/u/nelsonah)
#### Post date: [February 5, 2021, 8:10pm UTC](https://discourse.julialang.org/t/preallocate-an-array-of-tuples/54699/5 "2021-02-05T20:10:18Z")

</div>

All 3 suggestions worked like a charm and were great.  
I did have to change the points[2][3] = val % hangover from Python  
to points[2, 3] = val  
Many thanks. Harvey

---

<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: [February 5, 2021, 8:25pm UTC](https://discourse.julialang.org/t/preallocate-an-array-of-tuples/54699/6 "2021-02-05T20:25:53Z")

</div>

> [@nelsonah](#):
>
> I did have to change the points[2][3] = val % hangover from Python

Yeah, `numpy` lets you treat a matrix as if it were also a list of lists, but in Julia those are separate concepts and therefore separate types. It does take a bit of getting used to, but I think it’s worth it.

---

<div class="post-metadata">

### Author: ![nelsonah](https://avatars.discourse-cdn.com/v4/letter/n/7993a0/32.png) [@nelsonah](https://discourse.julialang.org/u/nelsonah)
#### Post date: [February 5, 2021, 8:30pm UTC](https://discourse.julialang.org/t/preallocate-an-array-of-tuples/54699/7 "2021-02-05T20:30:40Z")

</div>

I forgot to mention, Jliing hit a key issue. The elements of val were of type Int64, not UInt8.  
Again, many thanks.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [February 5, 2021, 8:38pm UTC](https://discourse.julialang.org/t/preallocate-an-array-of-tuples/54699/8 "2021-02-05T20:38:14Z")

</div>

> [@nelsonah](#):
>
> I did have to change the points[2][3] = val % hangover from Python

You can do this in Python, but I think that `points[2, 3]` is the most idiomatic numpy.
