# Concatenating two collections into a new one

**URL:** <https://discourse.julialang.org/t/concatenating-two-collections-into-a-new-one/742>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 5, 2016, 3:29pm UTC](https://discourse.julialang.org/t/concatenating-two-collections-into-a-new-one/742 "2016-12-05T15:29:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan\_Dolinsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jan_dolinsky/32/31347_2.png) [@Jan\_Dolinsky](https://discourse.julialang.org/u/Jan_Dolinsky)\
**Post date:** [December 5, 2016, 3:29pm UTC](https://discourse.julialang.org/t/concatenating-two-collections-into-a-new-one/742/1 "2016-12-05T15:29:05Z")

</div>

Hi,

I am trying to concatenate two collections of which each element is an object of a custom type resulting in a new collection. Is there any function which will do this for me ? E.g. I found `union(c1, c2)` which roughly does the job but I am interested in pure concatenation. I also found append!(c1, c2) which actually does the job but c1 get overwritten.

Thanks,  
Jan

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [December 5, 2016, 3:36pm UTC](https://discourse.julialang.org/t/concatenating-two-collections-into-a-new-one/742/2 "2016-12-05T15:36:41Z")

</div>

If you don’t want `c1` to be overwritten, can you `deepcopy` it?

```julia
c = deepcopy(c1)
append!(c, c2)

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 5, 2016, 3:46pm UTC](https://discourse.julialang.org/t/concatenating-two-collections-into-a-new-one/742/3 "2016-12-05T15:46:51Z")

</div>

> [@Jan\_Dolinsky](#):
>
> pure concatenation

What do you mean by that?

> [@mzaffalon](#):
>
> can you `deepcopy` it?

Do not use `deepcopy` for this. `copy` might be ok.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [December 5, 2016, 4:42pm UTC](https://discourse.julialang.org/t/concatenating-two-collections-into-a-new-one/742/4 "2016-12-05T16:42:37Z")

</div>

`union` is meant to be used with sets, it will get rid of any duplicated (non-unique) elements. `vcat` is probably what you want.

---

<div class="post-metadata">

**Author:** ![Jan\_Dolinsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jan_dolinsky/32/31347_2.png) [@Jan\_Dolinsky](https://discourse.julialang.org/u/Jan_Dolinsky)\
**Post date:** [December 5, 2016, 4:44pm UTC](https://discourse.julialang.org/t/concatenating-two-collections-into-a-new-one/742/5 "2016-12-05T16:44:57Z")

</div>

> pure concatenation

union(c1, c2) would repeat any element which is in c1 and also in c2 only once e.g.

```julia
c1 = ["a","b","c"]
c2 = ["b","c","d"]
union(c1, c2)
4-element Array{String,1}:
 "a"
 "b"
 "c"
 "d"

```

however the following concatenation return full 6-element array

```julia
[c1; c2]

6-element Array{String,1}:
 "a"
 "b"
 "c"
 "b"
 "c"
 "d"

```

This is the desired behaviour. I wondered whether there is a general function for concatenating two general collections. I will try:

```julia
cat(1, c1, c2)

```

and post results later.

Using copy() (or deepcopy()) and append!() would indeed work.

Thanks,  
Jan

---

<div class="post-metadata">

**Author:** ![Jan\_Dolinsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jan_dolinsky/32/31347_2.png) [@Jan\_Dolinsky](https://discourse.julialang.org/u/Jan_Dolinsky)\
**Post date:** [December 5, 2016, 4:46pm UTC](https://discourse.julialang.org/t/concatenating-two-collections-into-a-new-one/742/6 "2016-12-05T16:46:22Z")

</div>

Yes, indeed, `vcat()` seems like a solution. I’ll give it a shot tomorrow.

Thanks,  
Jan

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 5, 2016, 5:13pm UTC](https://discourse.julialang.org/t/concatenating-two-collections-into-a-new-one/742/7 "2016-12-05T17:13:57Z")

</div>

OK, it wasn’t clear from your original post what collection are you talking about. If you just mean `Vector`s then `vcat` is what you need. Note that it is exactly the same as `[a; b]`.

---

<div class="post-metadata">

**Author:** ![Jan\_Dolinsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jan_dolinsky/32/31347_2.png) [@Jan\_Dolinsky](https://discourse.julialang.org/u/Jan_Dolinsky)\
**Post date:** [December 6, 2016, 12:39pm UTC](https://discourse.julialang.org/t/concatenating-two-collections-into-a-new-one/742/8 "2016-12-06T12:39:57Z")

</div>

Thanks for all the helpful answers. I should have started with an example :). vcat() works well :

```julia
type myType
    name::String
    a::Int
end

c1 = [myType("a" * string(i), i) for i in rand(91:100, 5)]
c2 = [myType("a" * string(i), i) for i in rand(81:100, 5)]

c1
5-element Array{myType,1}:
 myType("a91",91)
 myType("a98",98)
 myType("a95",95)
 myType("a91",91)
 myType("a92",92)

c2
5-element Array{myType,1}:
 myType("a90",90)
 myType("a95",95)
 myType("a86",86)
 myType("a87",87)
 myType("a81",81)

vcat(c1, c2)
10-element Array{myType,1}:
 myType("a91",91)
 myType("a98",98)
 myType("a95",95)
 myType("a91",91)
 myType("a92",92)
 myType("a90",90)
 myType("a95",95)
 myType("a86",86)
 myType("a87",87)
 myType("a81",81)

```

I ruled out vcat() because I thought for some reason it is not applicable to collections. What is in general a difference between an array and a collection ?

Thanks,  
Jan

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [December 6, 2016, 12:46pm UTC](https://discourse.julialang.org/t/concatenating-two-collections-into-a-new-one/742/9 "2016-12-06T12:46:55Z")

</div>

> [@Jan\_Dolinsky](#):
>
> What is in general a difference between an array and a collection ?

`Array` (and therefore `Vector` and `Matrix`) is a particular kind of `AbstractArray`, which is itself a particular kind of collection. Other collections include `Set`, `Dict`, `Tuple` and `String`.

---

<div class="post-metadata">

**Author:** ![Jan\_Dolinsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jan_dolinsky/32/31347_2.png) [@Jan\_Dolinsky](https://discourse.julialang.org/u/Jan_Dolinsky)\
**Post date:** [December 6, 2016, 12:57pm UTC](https://discourse.julialang.org/t/concatenating-two-collections-into-a-new-one/742/10 "2016-12-06T12:57:32Z")

</div>

Thanks a lot for clarification.

Jan
