# Is there a many to one dictionary shorthand?

**URL:** <https://discourse.julialang.org/t/is-there-a-many-to-one-dictionary-shorthand/56837>\
**Category:** General Usage\
**Tags:** dictionary\
**Created:** [March 9, 2021, 11:27pm UTC](https://discourse.julialang.org/t/is-there-a-many-to-one-dictionary-shorthand/56837 "2021-03-09T23:27:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 9, 2021, 11:27pm UTC](https://discourse.julialang.org/t/is-there-a-many-to-one-dictionary-shorthand/56837/1 "2021-03-09T23:27:46Z")

</div>

Consider this named tuple, which could also be written as a dictionary  
(a = 1, b = 1, c = 2, d = 2)

Is there a shorthand to group keys with the same value eg?  
( (a,b) = 1, (c,d) = 2)

Could use an if - in statement. Just wondering if there’s a nice shorthand like above

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [March 9, 2021, 11:52pm UTC](https://discourse.julialang.org/t/is-there-a-many-to-one-dictionary-shorthand/56837/2 "2021-03-09T23:52:01Z")

</div>

I have been known to do:

```julia
julia> Dict([[:a, :b] .=> 1; [:c, :d] .=> 2])
Dict{Symbol, Int64} with 4 entries:
  :a => 1
  :b => 1
  :d => 2
  :c => 2

```

though this is not particularly efficent. (probably find if you are working so few elements you are writing literals)

How this works:  
`.=>` is broadcasted `Pair`

```julia
julia> x = [:a, :b] .=> 1
2-element Vector{Pair{Symbol, Int64}}:
 :a => 1
 :b => 1

julia> y = [:c, :d] .=> 2
2-element Vector{Pair{Symbol, Int64}}:
 :c => 2
 :d => 2

```

`;` insde `[]` is `vcat`

```julia
julia> [x; y]
4-element Vector{Pair{Symbol, Int64}}:
 :a => 1
 :b => 1
 :c => 2
 :d => 2

```

And then `Dict` can be constructed from a `Vector{<:Pair}`

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 10, 2021, 12:47am UTC](https://discourse.julialang.org/t/is-there-a-many-to-one-dictionary-shorthand/56837/3 "2021-03-10T00:47:57Z")

</div>

Thanks Lyndon  
That’s really cool. I see you can also do it with spaces and newlines instead of , and ;

```julia
Dict([  
        [:a :b] .=> 1
        [:c :d] .=> 2
     ])

```

---

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [March 10, 2021, 7:48am UTC](https://discourse.julialang.org/t/is-there-a-many-to-one-dictionary-shorthand/56837/4 "2021-03-10T07:48:31Z")

</div>

> [@Lincoln\_Hannah](#):
>
> I see you can also do it with spaces and newlines instead of , and ;

This has nothing to do with `Dict` or `Pair` but pertains to how an array (particularly a matrix) is built in Julia.

1. `[:a :b]` is a _row_ vector, and `[:a :b] .=> 1` produces also a row vector by broadcasting.

```julia
julia> [:a :b] .=> 1
1×2 Array{Pair{Symbol,Int64},2}:
 :a=>1 :b=>1

```

1. The newlines inside a `[]` works like `vcat`. In the above example, it means to concatenate two 1x2 vectors vertically into a 2x2 matrix.

```julia
julia> [[1 2]
        [3 4]
       ]
2×2 Array{Int64,2}:
 1 2
 3 4

julia> [
        [:a :b] .=> 1
        [:c :d] .=> 2
       ]
2×2 Array{Pair{Symbol,Int64},2}:
 :a=>1 :b=>1
 :c=>2 :d=>2

```

1. Since the resultant 2x2 matrix is iterable, the constructor `Dict([itr])` is called.

**More information.**  
Note that `[1, 2]` is a column vector while `[1 2]` is a row vector. The `;` is equivalent to a newline when building a matrix (also equal to `vcat`; if you want to write them in a single line).

```julia
julia> [[1, 2]; [2, 3]]
4-element Array{Int64,1}:
 1
 2
 2
 3

julia> vcat([1, 2], [3, 4])
4-element Array{Int64,1}:
 1
 2
 3
 4

```

while the _space_ between elements defines a row, or more generally, horizontal concatenation.

```julia
julia> [[1, 2] [3, 4]]
2×2 Array{Int64,2}:
 1 3
 2 4

julia> hcat([1, 2], [3, 4])
2×2 Array{Int64,2}:
 1 3
 2 4
julia> [1 3; 2 4]
2×2 Array{Int64,2}:
 1 3
 2 4

```

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 10, 2021, 10:18pm UTC](https://discourse.julialang.org/t/is-there-a-many-to-one-dictionary-shorthand/56837/5 "2021-03-10T22:18:09Z")

</div>

Thanks Shuhua. very useful
