# \`Pair{A,B}\` vs. \`Tuple{A,B}\`

**URL:** <https://discourse.julialang.org/t/pair-a-b-vs-tuple-a-b/942>\
**Category:** General Usage\
**Created:** [December 14, 2016, 1:59pm UTC](https://discourse.julialang.org/t/pair-a-b-vs-tuple-a-b/942 "2016-12-14T13:59:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [December 14, 2016, 1:59pm UTC](https://discourse.julialang.org/t/pair-a-b-vs-tuple-a-b/942/1 "2016-12-14T13:59:09Z")

</div>

So what’s the difference between `Pair{A,B}` and `Tuple{A,B}`? Why do we have both instead of one?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [December 14, 2016, 2:10pm UTC](https://discourse.julialang.org/t/pair-a-b-vs-tuple-a-b/942/2 "2016-12-14T14:10:57Z")

</div>

Types transport meaning. Same reason why we don’t always just use blobs of memory, even though everything boils down to that 😉  
E.g. you want to construct a pair with this syntax: `a => b`, but adding this constructor to Tuples won’t make much sense!

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [December 14, 2016, 2:48pm UTC](https://discourse.julialang.org/t/pair-a-b-vs-tuple-a-b/942/3 "2016-12-14T14:48:47Z")

</div>

Tuples are much more low-level, being one of the few parts that are defined in non-Julia code in the `src` directory. They also have some slightly different behaviours than other types, such as allowing an arbitrary number of arguments, and being covariant,

```julia
julia> isa((1,3),Tuple{Real,Real})
true

julia> isa(1=>3,Pair{Real,Real})
false

```

Pairs on the other hand are just a [regular immutable type](https://github.com/JuliaLang/julia/blob/bfc72b6fe913236ccb2bdd1ef254bceccb0cc31a/base/operators.jl#L955-L958).

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [May 15, 2021, 4:09pm UTC](https://discourse.julialang.org/t/pair-a-b-vs-tuple-a-b/942/4 "2021-05-15T16:09:59Z")

</div>

I’m quite new to Julia. I found this thread trying to understand the difference between Pair and Tuple. While some of my question have been answered above, I still have these three:

1. Why is Dict based on Pair, not Tuple ? (Perhaps covariance is bad for Dict ? if I understand what covariance means from simonbyrne’s post.)

2. How is Pair used outside Dict contexts?

3. Is covariance the only fundamental semantic difference?

Questions 2 & 3 boil down to, basically, in what context should I prefer one over the other when both seem to work?

---

<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:** [May 20, 2021, 1:07pm UTC](https://discourse.julialang.org/t/pair-a-b-vs-tuple-a-b/942/5 "2021-05-20T13:07:46Z")

</div>

I don’t think that (lack of) covariance motivated the implementation of `Dict`, `Pair` is simply convenient with the `=>` syntax. In any case, note that internally `Dict` does not use `Pair`, it is just for construction.

`Pair` is generally used in contexts where you need to store a pair of objects that belong together.

I would mostly use `Pair` to signify intent, and for dispatch. Eg the `Dict(::Pair...)` constructor is different from the fallback `Dict(itr)`.
