# Named tuples

**URL:** https://discourse.julialang.org/t/named-tuples/41061
**Category:** New to Julia
**Created:** [June 9, 2020, 11:31am UTC](https://discourse.julialang.org/t/named-tuples/41061 "2020-06-09T11:31:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [June 9, 2020, 11:31am UTC](https://discourse.julialang.org/t/named-tuples/41061/1 "2020-06-09T11:31:18Z")

</div>

I am experimenting with named tuples to store my parameters for code readability. I have noticed that although one can create a tuple with one element, it cannot be accessed via the dot notation:

```julia
a = (b=3)

```

returns

```julia
julia> a.b
ERROR: type Int64 has no field b
Stacktrace:
 [1] getproperty(::Int64, ::Symbol) at ./Base.jl:33

```

On the other hand, a tuple with two elements:

```julia
a = (b=3, c=4)

```

handles perfectly fine:

```julia
a.b

```

returns `3` as expected.

Is this expected behavior? What is the rationale? In general, I might wish to store parameters in a named tuple (perhaps a named array in the future), and at times, there might only be a single parameter.

Thanks.

---

<div class="post-metadata">

### Author: ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)
#### Post date: [June 9, 2020, 11:35am UTC](https://discourse.julialang.org/t/named-tuples/41061/2 "2020-06-09T11:35:00Z")

</div>

The syntax for tuple creation requires at least one comma, otherwise the parantheses are just used for precedence:  
`(b=3,)`

---

<div class="post-metadata">

### Author: ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)
#### Post date: [June 9, 2020, 1:59pm UTC](https://discourse.julialang.org/t/named-tuples/41061/3 "2020-06-09T13:59:25Z")

</div>

To expand on this answer:

```julia
julia> a = (b=3)
3

julia> typeof(a)
Int64

julia> a = (b=3,)
(b = 3,)

julia> typeof(a)
NamedTuple{(:b,),Tuple{Int64}}

julia> a.b
3

```

---

<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: [June 9, 2020, 2:01pm UTC](https://discourse.julialang.org/t/named-tuples/41061/4 "2020-06-09T14:01:50Z")

</div>

I was taught (in Python, but it applies here too) to think of the comma as the tuple operator, not the parentheses. I’ve found that lesson helpful in cases like this.

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [June 9, 2020, 3:31pm UTC](https://discourse.julialang.org/t/named-tuples/41061/5 "2020-06-09T15:31:08Z")

</div>

Thank you all! Regarding use of the comma:

```julia
a = 3,

```

does not work as intended. One has to use

```julia
a = (3,)

```

But that is hardly an issue.

---

<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: [June 9, 2020, 3:38pm UTC](https://discourse.julialang.org/t/named-tuples/41061/6 "2020-06-09T15:38:33Z")

</div>

You need both, otherwise how would you distinguish between

```julia
a = (3,)

```

and

```julia
(a = 3,)

```

?

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [June 9, 2020, 6:46pm UTC](https://discourse.julialang.org/t/named-tuples/41061/7 "2020-06-09T18:46:42Z")

</div>

For tuples you can write `tuple(3)` if you think the comma is too subtle, it’s a pity that `tuple(a = 3)` doesn’t make a named tuple.
