# Collect(a) vs. \[a;\]

**URL:** <https://discourse.julialang.org/t/collect-a-vs-a/47055>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 22, 2020, 9:09am UTC](https://discourse.julialang.org/t/collect-a-vs-a/47055 "2020-09-22T09:09:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)\
**Post date:** [September 22, 2020, 9:09am UTC](https://discourse.julialang.org/t/collect-a-vs-a/47055/1 "2020-09-22T09:09:43Z")

</div>

Hi,  
I don’t get the difference between `collect(a)` and `[a;]` for ranges.

```nohighlight
julia> typeof(collect(5:7)) === typeof([5:7;])
true

julia> collect(5:7) == [5:7;]
true

julia> collect(5:7)
3-element Array{Int64,1}:
 5
 6
 7

julia> [5:7;]
3-element Array{Int64,1}:
 5
 6
 7

```

Yet

```nohighlight
julia> collect(5:7) === [5:7;]
false

```

Is it only because:

```nohighlight
julia> @which [5:7;]
vcat(rs::AbstractRange{T}...) where T in Base at range.jl:911

julia> @which collect(5:7)
collect(r::AbstractRange) in Base at range.jl:925

```

Or maybe I just misunderstood `===`.

Moreover, for:

```nohighlight
julia> A = Dict([5 => 2, 12 => 9])
Dict{Int64,Int64} with 2 entries:
  5 => 2
  12 => 9

julia> B = keys(A)
Base.KeySet for a Dict{Int64,Int64} with 2 entries. Keys:
  5
  12

```

I expected `[B;]` to return the same as `collect(B)` yet it doesn’t:

```nohighlight
julia> [B;]
1-element Array{Base.KeySet{Int64,Dict{Int64,Int64}},1}:
 [5, 12]

```

Is there a reason for that behavior?  
Thanks in advance!  
Rémi

**EDIT:**  
It seems to do exactly the same. I will take a look at the function definition.

```nohighlight
julia> @benchmark collect(i:j) setup=((i,j)=(rand(1:100), rand(1:100)))
BenchmarkTools.Trial: 
  memory estimate: 80 bytes
  allocs estimate: 1
  --------------
  minimum time: 35.413 ns (0.00% GC)
  median time: 45.078 ns (0.00% GC)
  mean time: 79.823 ns (26.02% GC)
  maximum time: 50.168 μs (99.50% GC)
  --------------
  samples: 10000
  evals/sample: 993

julia> @benchmark [i:j;] setup=((i,j)=(rand(1:100), rand(1:100)))
BenchmarkTools.Trial: 
  memory estimate: 80 bytes
  allocs estimate: 1
  --------------
  minimum time: 35.374 ns (0.00% GC)
  median time: 45.647 ns (0.00% GC)
  mean time: 79.846 ns (25.69% GC)
  maximum time: 50.445 μs (99.76% GC)
  --------------
  samples: 10000
  evals/sample: 993

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 22, 2020, 9:22am UTC](https://discourse.julialang.org/t/collect-a-vs-a/47055/2 "2020-09-22T09:22:35Z")

</div>

I start with the easy one:

> [@remi-garcia](#):
>
> Or maybe I just misunderstood `===` .

Yes I think so, e.g.:

```julia
julia> [5,6,7]==[5,6,7]
true

julia> [5,6,7]===[5,6,7]
false

help?> ===
search: === == !==

  ===(x,y) -> Bool
  ≡(x,y) -> Bool

  Determine whether x and y are identical, in the sense that no program could distinguish them. First the types of x and y are compared. If those are identical, mutable
  objects are compared by address in memory and immutable objects (such as numbers) are compared by contents at the bit level. This function is sometimes called "egal". It
  always returns a Bool value.

julia> ismutable([5,6,7])
true

```

Array are mutable objects and are therefore compared by address in memory. Thats why above `===` comparison gives false.

---

<div class="post-metadata">

**Author:** ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)\
**Post date:** [September 22, 2020, 9:25am UTC](https://discourse.julialang.org/t/collect-a-vs-a/47055/3 "2020-09-22T09:25:59Z")

</div>

Of course! That’s a strange thing that I wanted `===` to be `==` + type equality 😕

This answer my main question, thanks!

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 22, 2020, 9:35am UTC](https://discourse.julialang.org/t/collect-a-vs-a/47055/4 "2020-09-22T09:35:20Z")

</div>

About collect:  
[https://docs.julialang.org/en/v1/manual/arrays/#man-array-concatenation](https://docs.julialang.org/en/v1/manual/arrays/#man-array-concatenation)

> If the arguments inside the square brackets are separated by semicolons ( `;` ) or newlines instead of commas, then their contents are _vertically concatenated_ together instead of the arguments being used as elements themselves.

```julia
julia> @code_lowered collect(5:7)
CodeInfo(
1 ─ %1 = Base.vcat(r)
└── return %1
)

```

As you can see: `collect` in this case is just a `vcat`, so identical to `[5:7;]`.

---

<div class="post-metadata">

**Author:** ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)\
**Post date:** [September 22, 2020, 11:36am UTC](https://discourse.julialang.org/t/collect-a-vs-a/47055/5 "2020-09-22T11:36:59Z")

</div>

Ok, that makes sense!  
It seems like `collect(a)` should be preferred over `[a;]`:

```nohighlight
julia> @code_lowered [5:7;]
CodeInfo(
911 1 ── Core.NewvarNode(:(a)) │
    │ Core.NewvarNode(:(i)) │
    │ Core.NewvarNode(:(#temp#@_7)) │
    │ %4 = (Base.convert)(Base.Int, 0) │
    │ n = (Core.typeassert)(%4, Base.Int) │
912 │ %6 = rs │
    │ #temp#@_4 = (Base.iterate)(%6) │
    │ %8 = #temp#@_4 === nothing │
    │ %9 = (Base.not_int)(%8) │
    └─── goto #4 if not %9 │
    2 ┄─ %11 = #temp#@_4 │
    │ ra@_13 = (Core.getfield)(%11, 1) │
    │ %13 = (Core.getfield)(%11, 2) │
913 │ %14 = n │
    │ %15 = (Base.length)(ra@_13) │
    │ %16 = %14 + %15 │
    │ %17 = (Base.convert)(Base.Int, %16) │
    │ n = (Core.typeassert)(%17, Base.Int) │
    │ #temp#@_4 = (Base.iterate)(%6, %13) │
    │ %20 = #temp#@_4 === nothing │
    │ %21 = (Base.not_int)(%20) │
    └─── goto #4 if not %21 │
    3 ── goto #2 │
915 4 ── %24 = (Core.apply_type)(Base.Vector, $(Expr(:static_parameter, 1))) │
    │ a = (%24)(Base.undef, n) │
916 │ i = 1 │
917 │ %27 = rs │
    │ #temp#@_7 = (Base.iterate)(%27) │
    │ %29 = #temp#@_7 === nothing │
    │ %30 = (Base.not_int)(%29) │
    └─── goto #10 if not %30 │
    5 ┄─ %32 = #temp#@_7 │
    │ ra@_9 = (Core.getfield)(%32, 1) │
    │ %34 = (Core.getfield)(%32, 2) │
    │ %35 = ra@_9 │
    │ #temp#@_8 = (Base.iterate)(%35) │
    │ %37 = #temp#@_8 === nothing │
    │ %38 = (Base.not_int)(%37) │
    └─── goto #8 if not %38 │
    6 ┄─ %40 = ra@_9 │
    │ ra@_12 = %40 │
    │ %42 = #temp#@_8 │
    │ x = (Core.getfield)(%42, 1) │
    │ %44 = (Core.getfield)(%42, 2) │
918 │ $(Expr(:inbounds, true)) │
    │ (Base.setindex!)(a, x, i) │
    │ val = x │
    │ $(Expr(:inbounds, :pop)) │
    │ val │
919 │ i = i + 1 │
    │ #temp#@_8 = (Base.iterate)(%35, %44) │
    │ %52 = #temp#@_8 === nothing │
    │ %53 = (Base.not_int)(%52) │
    └─── goto #8 if not %53 │
    7 ── goto #6 │
    8 ── #temp#@_7 = (Base.iterate)(%27, %34) │
    │ %57 = #temp#@_7 === nothing │
    │ %58 = (Base.not_int)(%57) │
    └─── goto #10 if not %58 │
    9 ── goto #5 │
921 10 ─ return a │
)

julia> @code_lowered collect(5:7)
CodeInfo(
925 1 ─ %1 = (Base.vcat)(r) │
    └── return %1 │
)

```

Or not!

```nohighlight
julia> @code_lowered vcat(5:7)
CodeInfo(
911 1 ── Core.NewvarNode(:(a)) │
    │ Core.NewvarNode(:(i)) │
    │ Core.NewvarNode(:(#temp#@_7)) │
    │ %4 = (Base.convert)(Base.Int, 0) │
    │ n = (Core.typeassert)(%4, Base.Int) │
912 │ %6 = rs │
    │ #temp#@_4 = (Base.iterate)(%6) │
    │ %8 = #temp#@_4 === nothing │
    │ %9 = (Base.not_int)(%8) │
    └─── goto #4 if not %9 │
    2 ┄─ %11 = #temp#@_4 │
    │ ra@_13 = (Core.getfield)(%11, 1) │
    │ %13 = (Core.getfield)(%11, 2) │
913 │ %14 = n │
    │ %15 = (Base.length)(ra@_13) │
    │ %16 = %14 + %15 │
    │ %17 = (Base.convert)(Base.Int, %16) │
    │ n = (Core.typeassert)(%17, Base.Int) │
    │ #temp#@_4 = (Base.iterate)(%6, %13) │
    │ %20 = #temp#@_4 === nothing │
    │ %21 = (Base.not_int)(%20) │
    └─── goto #4 if not %21 │
    3 ── goto #2 │
915 4 ── %24 = (Core.apply_type)(Base.Vector, $(Expr(:static_parameter, 1))) │
    │ a = (%24)(Base.undef, n) │
916 │ i = 1 │
917 │ %27 = rs │
    │ #temp#@_7 = (Base.iterate)(%27) │
    │ %29 = #temp#@_7 === nothing │
    │ %30 = (Base.not_int)(%29) │
    └─── goto #10 if not %30 │
    5 ┄─ %32 = #temp#@_7 │
    │ ra@_9 = (Core.getfield)(%32, 1) │
    │ %34 = (Core.getfield)(%32, 2) │
    │ %35 = ra@_9 │
    │ #temp#@_8 = (Base.iterate)(%35) │
    │ %37 = #temp#@_8 === nothing │
    │ %38 = (Base.not_int)(%37) │
    └─── goto #8 if not %38 │
    6 ┄─ %40 = ra@_9 │
    │ ra@_12 = %40 │
    │ %42 = #temp#@_8 │
    │ x = (Core.getfield)(%42, 1) │
    │ %44 = (Core.getfield)(%42, 2) │
918 │ $(Expr(:inbounds, true)) │
    │ (Base.setindex!)(a, x, i) │
    │ val = x │
    │ $(Expr(:inbounds, :pop)) │
    │ val │
919 │ i = i + 1 │
    │ #temp#@_8 = (Base.iterate)(%35, %44) │
    │ %52 = #temp#@_8 === nothing │
    │ %53 = (Base.not_int)(%52) │
    └─── goto #8 if not %53 │
    7 ── goto #6 │
    8 ── #temp#@_7 = (Base.iterate)(%27, %34) │
    │ %57 = #temp#@_7 === nothing │
    │ %58 = (Base.not_int)(%57) │
    └─── goto #10 if not %58 │
    9 ── goto #5 │
921 10 ─ return a │
)

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 22, 2020, 11:39am UTC](https://discourse.julialang.org/t/collect-a-vs-a/47055/6 "2020-09-22T11:39:41Z")

</div>

They are the same. Both is at the end `vcat`.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [September 22, 2020, 11:49am UTC](https://discourse.julialang.org/t/collect-a-vs-a/47055/7 "2020-09-22T11:49:59Z")

</div>

> [@remi-garcia](#):
>
> It seems like `collect(a)` should be preferred over `[a;]`

Yeah, personally I would go for `collect`, since turning an iterable into a `Vector` (or more generally an `Array`) is exactly its purpose, so in my opinion the code is more readable that way. I wouldn’t really worry about comparing implementations unless it turned out to be a performance bottleneck (in which case you might want to avoid allocating arrays anyway, depending on the situation).

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 22, 2020, 12:10pm UTC](https://discourse.julialang.org/t/collect-a-vs-a/47055/8 "2020-09-22T12:10:10Z")

</div>

> [@ericphanson](#):
>
> in my opinion the code is more readable that way

I see it the same way. Actually I had to look up for the `[5:7;]` syntax because I wasn’t aware of this subtilty.

---

<div class="post-metadata">

**Author:** ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)\
**Post date:** [September 22, 2020, 12:36pm UTC](https://discourse.julialang.org/t/collect-a-vs-a/47055/9 "2020-09-22T12:36:38Z")

</div>

I agree!  
This notation is used in julia testings:  
[https://github.com/JuliaLang/julia/blob/46cf572773b05d8f4ab74705b139f7a921227c02/test/sorting.jl#L36](https://github.com/JuliaLang/julia/blob/46cf572773b05d8f4ab74705b139f7a921227c02/test/sorting.jl#L36)

---

<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:** [September 22, 2020, 2:39pm UTC](https://discourse.julialang.org/t/collect-a-vs-a/47055/10 "2020-09-22T14:39:42Z")

</div>

> [@remi-garcia](#):
>
> This notation is used in julia testings

Code should ideally signal intent, not rely on a corner case. I think that `collect` is vastly preferable.

---

<div class="post-metadata">

**Author:** ![remi-garcia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/remi-garcia/32/7125_2.png) [@remi-garcia](https://discourse.julialang.org/u/remi-garcia)\
**Post date:** [September 22, 2020, 4:15pm UTC](https://discourse.julialang.org/t/collect-a-vs-a/47055/11 "2020-09-22T16:15:49Z")

</div>

I also agree and opened an issue to suggest the change ([https://github.com/JuliaLang/julia/issues/37698](https://github.com/JuliaLang/julia/issues/37698))
