# Pair out of nowhere (I wanted a dictionary)

**URL:** <https://discourse.julialang.org/t/pair-out-of-nowhere-i-wanted-a-dictionary/25895>\
**Category:** New to Julia\
**Tags:** dictionary\
**Created:** [July 1, 2019, 2:56pm UTC](https://discourse.julialang.org/t/pair-out-of-nowhere-i-wanted-a-dictionary/25895 "2019-07-01T14:56:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dburch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dburch/32/8741_2.png) [@dburch](https://discourse.julialang.org/u/dburch)\
**Post date:** [July 1, 2019, 2:56pm UTC](https://discourse.julialang.org/t/pair-out-of-nowhere-i-wanted-a-dictionary/25895/1 "2019-07-01T14:56:51Z")

</div>

Pairs are driving me a little bit crazy here. I don’t really understand the difference between a Dictionary and a Pair, and I don’t mentally see how a Dictionary type is inadequate. See this example. I’m trying to create a second Dictionary within the first dictionary, but I am somehow creating a Pair even though I am using the exact same syntax to create a Dictionary. Please help. What am I doing wrong?

```julia
julia> orderList = Dict{Int,Dict{Int,Int}}()
Dict{Int64,Dict{Int64,Int64}} with 0 entries

julia> push!(orderList, 2222201 => Dict())
Dict{Int64,Dict{Int64,Int64}} with 1 entry:
  2222201 => Dict{Int64,Int64}()

julia> orderList[2222201] = 1 => 22222
ERROR: MethodError: Cannot `convert` an object of type Pair{Int64,Int64} to an object of type Dict{Int64,Int64}
Closest candidates are:
  convert(::Type{T<:AbstractDict}, ::T<:AbstractDict) where T<:AbstractDict at abstractdict.jl:487
  convert(::Type{T<:AbstractDict}, ::AbstractDict) where T<:AbstractDict at abstractdict.jl:490
  convert(::Type{S}, ::T<:(Union{CategoricalString{R}, CategoricalValue{T,R} where
T} where R)) where {S, T<:(Union{CategoricalString{R}, CategoricalValue{T,R} where
T} where R)} at C:\Users\dburch.CSWG\.juliapro\JuliaPro_v1.0.4.1\packages\CategoricalArrays\JFETm\src\value.jl:91
  ...
Stacktrace:
 [1] setindex!(::Dict{Int64,Dict{Int64,Int64}}, ::Pair{Int64,Int64}, ::Int64) at .\dict.jl:381
 [2] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [July 1, 2019, 3:01pm UTC](https://discourse.julialang.org/t/pair-out-of-nowhere-i-wanted-a-dictionary/25895/2 "2019-07-01T15:01:03Z")

</div>

```julia
orderList[2222201] = Dict(1 => 22222)

```

---

<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:** [July 1, 2019, 3:01pm UTC](https://discourse.julialang.org/t/pair-out-of-nowhere-i-wanted-a-dictionary/25895/3 "2019-07-01T15:01:13Z")

</div>

You can think of a dictionary as a _collection_ of pairs.

```julia
julia> d = Dict{Int, Int}()
Dict{Int64,Int64} with 0 entries

# eltype(d) gives the type of each *element* of d:
julia> eltype(d)
Pair{Int64,Int64}

```

The syntax `=>` constructs a `Pair`:

```julia
julia> typeof(1 => 2)
Pair{Int64,Int64}

```

You can construct a dictionary from one or more pairs:

```julia
julia> d = Dict(1 => 2, 3 => 4, 5 => 6)
Dict{Int64,Int64} with 3 entries:
  3 => 4
  5 => 6
  1 => 2

```

but a pair is not the same as a dictionary in the same way that an `Int` is not the same as a `Vector{Int}`.

So in your example, you need:

```julia
julia> orderList[2222201] = Dict(1 => 2222)
Dict{Int64,Int64} with 1 entry:
  1 => 2222

```

or

```julia
julia> orderList[2222201][1] = 2222
2222

```
