# Initialize Arrays from Ranges, Semicolon explanation

**URL:** <https://discourse.julialang.org/t/initialize-arrays-from-ranges-semicolon-explanation/22559>\
**Category:** New to Julia\
**Created:** [March 31, 2019, 3:22am UTC](https://discourse.julialang.org/t/initialize-arrays-from-ranges-semicolon-explanation/22559 "2019-03-31T03:22:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![spectramax](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@spectramax](https://discourse.julialang.org/u/spectramax)\
**Post date:** [March 31, 2019, 3:22am UTC](https://discourse.julialang.org/t/initialize-arrays-from-ranges-semicolon-explanation/22559/1 "2019-03-31T03:22:27Z")

</div>

Hello,

I’ve just read that I can initialize arrays from a range using the following syntax:

```julia
my_array = [1:10;]

```

What is the semicolon doing in this expression?

I thought that the `;` can be used to separate rows as follows:  
`my_array = [1; 2; 3; 4; 5]`

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [March 31, 2019, 3:42am UTC](https://discourse.julialang.org/t/initialize-arrays-from-ranges-semicolon-explanation/22559/2 "2019-03-31T03:42:38Z")

</div>

The semicolon denotes [vertical concatenation](https://docs.julialang.org/en/v1/manual/arrays/index.html#Concatenation-1). Separating elements with commas will make a list of those elements, while separating elements with semicolons or spaces will concatenate them — vertically for semicolons, and horizontally for spaces.

---

<div class="post-metadata">

**Author:** ![spectramax](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@spectramax](https://discourse.julialang.org/u/spectramax)\
**Post date:** [March 31, 2019, 6:26am UTC](https://discourse.julialang.org/t/initialize-arrays-from-ranges-semicolon-explanation/22559/3 "2019-03-31T06:26:29Z")

</div>

Yes, that makes sense. However, that is not my question. I was expecting this code to work:

```julia
my_array = [1:10]

```

but it makes a UnitRange instead of an Array{Int64}

so, again, what is the significance of the semicolon:

```julia
my_array = [1:10;]

```

Based on your answer, it should be vertically concatenating…but in this case of a range, what exactly is it vertically concatenating!?

---

<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:** [March 31, 2019, 6:49am UTC](https://discourse.julialang.org/t/initialize-arrays-from-ranges-semicolon-explanation/22559/4 "2019-03-31T06:49:10Z")

</div>

You should read the [docs](https://docs.julialang.org/en/v1/manual/arrays/#Concatenation-1), and this discussion:

> [@How to create a 2x1 Array?](https://discourse.julialang.org/t/how-to-create-a-2x1-array/8337/44):
>
> It was discussed in the lead up to 1.0 whether [1; 2; 3] should construct a vector or a matrix. But note that there’s nothing special about two dimensions in concatenation syntax in general: julia\> [rand(2); rand(2)] 4-element Array{Float64,1}: 0.528860561644692 0.2992626181507161 0.6659761151342882 0.423705081212141 julia\> [rand(2, 2, 2); rand(2, 2, 2)] 4×2×2 Array{Float64,3}: [:, :, 1] = 0.91018 0.0499205 0.136608 0.128014 0.621301 0.0150574 0.58328 0.694085 [:, :, 2] = 0.533…

That said, it is very likely that you want to use `1:10` _as is_ (without allocating an array), or if you want to modify values, `collect(1:10)`.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [March 31, 2019, 3:53pm UTC](https://discourse.julialang.org/t/initialize-arrays-from-ranges-semicolon-explanation/22559/5 "2019-03-31T15:53:55Z")

</div>

> [@spectramax](#):
>
> Based on your answer, it should be vertically concatenating…but in this case of a range, what exactly is it vertically concatenating!?

As @Tamas_Papp noted, you should probably use `1:10` as is or `collect` it. But just to answer this question: `[1:10]` is not a unit range, but rather a vector with the single element `1:10` (which is a `UnitRange`).  
`[1:10;]` is the same as `vcat(1:10)`. `vcat` returns the concatenated result as an `Array`, so in this case of a single argument, it simply converts it to an array (the fact that `vcat` return an `Array` does not seem to be documented, so I suppose it _could_ just as well have returned its argument `1:10` unmodified).

---

<div class="post-metadata">

**Author:** ![spectramax](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@spectramax](https://discourse.julialang.org/u/spectramax)\
**Post date:** [March 31, 2019, 4:01pm UTC](https://discourse.julialang.org/t/initialize-arrays-from-ranges-semicolon-explanation/22559/6 "2019-03-31T16:01:30Z")

</div>

@yha Yea, I should have and meant to say an Array of 1-element UnitRange. I posted this question on StackOverflow and I got an interesting answer.

Also, my question wasn’t whether I can use collect to simplify things. I am not looking for an answer about how to create arrays from ranges, per se. I wanted to get to the bottom of the _syntax_. Sorry, I have not made it clear enough.

[https://stackoverflow.com/questions/55438134/creating-arrays-from-ranges-in-julia-without-using-collect](https://stackoverflow.com/questions/55438134/creating-arrays-from-ranges-in-julia-without-using-collect)

I found the answer to be quite fascinating and staisfying. Especially, using the Debugger to get into the weeds of Vector() function.
