# Correct way to do multiple variable assignments

**URL:** <https://discourse.julialang.org/t/correct-way-to-do-multiple-variable-assignments/30242>\
**Category:** New to Julia\
**Created:** [October 23, 2019, 6:32pm UTC](https://discourse.julialang.org/t/correct-way-to-do-multiple-variable-assignments/30242 "2019-10-23T18:32:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![duncanam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/duncanam/32/10669_2.png) [@duncanam](https://discourse.julialang.org/u/duncanam)\
**Post date:** [October 23, 2019, 6:32pm UTC](https://discourse.julialang.org/t/correct-way-to-do-multiple-variable-assignments/30242/1 "2019-10-23T18:32:24Z")

</div>

I am trying to find out the right way to do something like this:

```julia
a,b,c = 1,2,3

```

or perhaps

```julia
a,b,c = (1,2,3)

```

I am seeing that [this thread](https://discourse.julialang.org/t/assignment-to-multiple-targets-how-does-it-work/11721) says that the left hand side is actually being interpreted as

```julia
(a,b,c) = ...

```

so therefore

```julia
(a,b,c) = (1,2,3) 

```

is also valid. Is there a better way to do assignment like this, or are any of the above okay?

---

<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:** [October 23, 2019, 6:41pm UTC](https://discourse.julialang.org/t/correct-way-to-do-multiple-variable-assignments/30242/2 "2019-10-23T18:41:33Z")

</div>

Those all look fine to me, and they all do the same thing. The first one is probably the most common, but you should use whichever one makes your code easiest to read and understand (at least for you). In all cases you’re assigning a Tuple of items on the right-hand side of the `=` sign to a `Tuple` of variables on the left-hand side.

---

<div class="post-metadata">

**Author:** ![j2b2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j2b2/32/3387_2.png) [@j2b2](https://discourse.julialang.org/u/j2b2)\
**Post date:** [March 15, 2021, 10:40am UTC](https://discourse.julialang.org/t/correct-way-to-do-multiple-variable-assignments/30242/3 "2021-03-15T10:40:33Z")

</div>

Hi, with a boolean `cond` I often write conditional assignment like

```julia
julia> cond = true
true

julia> cond && (a = 1)
1

```

but today I wrote a multiple assignment:

```julia
cond && (a, b = 1, 2)
ERROR: syntax: invalid named tuple element "2" around REPL[22]:1

```

Also I tried:

```julia
julia> cond && (a,b=[4,5])
(a = 1, b = [4, 5])

julia> a,b
(1, 2)

```

Sincerely, I’m a little lost with these named tuples:-(

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [March 15, 2021, 11:42am UTC](https://discourse.julialang.org/t/correct-way-to-do-multiple-variable-assignments/30242/4 "2021-03-15T11:42:58Z")

</div>

Anything with commas inside parenthesis is interpreted as a tuple (or a named tuple if it contains `=`), so that’s why that does not work.

Instead of `cond && (a, b = 1, 2)` write:

```julia
julia> cond && begin a, b = 1, 2 end
(1, 2)

julia> a
1

julia> b
2

```

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [March 15, 2021, 12:18pm UTC](https://discourse.julialang.org/t/correct-way-to-do-multiple-variable-assignments/30242/5 "2021-03-15T12:18:08Z")

</div>

You can of course also add more parentheses:

```julia
julia> true && ((a,b) = (1,2))
(1, 2)

julia> a
1

julia> b
2

```

---

<div class="post-metadata">

**Author:** ![j2b2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j2b2/32/3387_2.png) [@j2b2](https://discourse.julialang.org/u/j2b2)\
**Post date:** [March 15, 2021, 12:26pm UTC](https://discourse.julialang.org/t/correct-way-to-do-multiple-variable-assignments/30242/6 "2021-03-15T12:26:23Z")

</div>

heliosdrm && pfitzseb && (many thanks)
