# Finding the set of keys with the same value in a Dictionnary

**URL:** <https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578>\
**Category:** General Usage\
**Tags:** juliapro\
**Created:** [April 27, 2018, 3:14pm UTC](https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578 "2018-04-27T15:14:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)\
**Post date:** [April 27, 2018, 3:14pm UTC](https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578/1 "2018-04-27T15:14:20Z")

</div>

Hello, I know that a dictionary is not intended to be used this way but I’d like to know if it exists some function that allows me to get all the keys that have the same value in a dict. For example :

```julia
d = Dict( "dinner 1" => "carrot", "dinner 2" => "rice", "dinner 3" => "carrot") 

```

Then, if I want to find all the keys that contains “carrot” I would have : [“dinner 1”, “dinner 3”]

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [April 27, 2018, 3:30pm UTC](https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578/2 "2018-04-27T15:30:13Z")

</div>

```julia
julia> [k for (k,v) in d if v=="carrot"]
2-element Array{String,1}:
 "dinner 1"
 "dinner 3"

```

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [April 27, 2018, 4:00pm UTC](https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578/3 "2018-04-27T16:00:58Z")

</div>

```julia
dinners = Dict( "dinner 1" => "carrot", "dinner 2" => "rice", "dinner 3" => "carrot")
menus = Dict()
for (k, v) in dinners
    if haskey(menus, v)
        push!(menus[v],k)
    else
        menus[v] = [k]
    end
end

```

```julia
julia> menus
Dict{Any,Any} with 2 entries:
  "rice" => String["dinner 2"]
  "carrot" => String["dinner 1", "dinner 3"]
```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [April 28, 2018, 1:17pm UTC](https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578/4 "2018-04-28T13:17:14Z")

</div>

Alternatively, you can use `filter()` to return your results as a dictionary. For example:

`result = filter((k,v)->v == "carrot",d)`

Use keys(result) if you want the keys. If you want to find all of the keys that match unique values, you can also do this:

```julia
f(m,d) = filter((k,v)->v == m,d)
results = map(x->f(x,d),unique(values(d)))

```

`f(m,d)` makes the filter function more general and `map()` allows you to apply it to every unique value in the dictionary. With this method, each unique value is assigned to a distinct dictionary in the result array, which may or may not suit your purposes.

---

<div class="post-metadata">

**Author:** ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)\
**Post date:** [April 30, 2018, 8:24am UTC](https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578/5 "2018-04-30T08:24:45Z")

</div>

Thanks a lot for your suggestions. They work very well . However, I tried to use it in another version of a dictionary that looks like as follows :

```julia
d = Dict( "dinner 1" => ["carrot", "beans"], "dinner 2" => "rice", "dinner 3" => "carrot") 

```

In this way, we cannot get the correct keys that contains “carrot” because the instructions see the value together (the value of “dinner 1” it’s the full array [“carrot”, “beans”] and not separated), for example, the instruction

```julia
key_value = [k for (k,v) in d if v=="carrot"]

```

It will return only the key “dinner 3”. Do you think, it’s a way to get the values individually, even if the value associated to a key it’s a array of string ? I’d like to get the answer : “dinner 3” and “dinner 1”. I have this problem in other codes but I couldn’t find a way to deal with it. Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [April 30, 2018, 8:51am UTC](https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578/6 "2018-04-30T08:51:48Z")

</div>

Try something like this:

```julia
julia> has(x::String, y) = x == y
has (generic function with 1 method)

julia> has(x::AbstractArray, y) = y in x
has (generic function with 2 methods)

julia> key_value = [k for (k,v) in d if has(v, "carrot")]
2-element Array{String,1}:
 "dinner 1"
 "dinner 3"

```

---

<div class="post-metadata">

**Author:** ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)\
**Post date:** [April 30, 2018, 10:34am UTC](https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578/7 "2018-04-30T10:34:17Z")

</div>

Thanks a lot. Your function helped me a lot. My dict contains SubString{String} types and I’ve tried to use the following command to try to get these values :

```julia
has(x::SubString{String}, y) = y in x 

```

But unfortunately the function doesn’t return as expected. It keeps getting only the values that are String. Have you any idea why this behavior ?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [April 30, 2018, 10:36am UTC](https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578/8 "2018-04-30T10:36:03Z")

</div>

`SubString{String} <: String` is false, so you could either use `has(x::AbstractString, y) = x == y` or just `has(x, y) = x == y`.

---

<div class="post-metadata">

**Author:** ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)\
**Post date:** [April 30, 2018, 10:47am UTC](https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578/9 "2018-04-30T10:47:10Z")

</div>

Thanks for your answer. Unfortunately, It didn’t work. In fact, I have a dictionnary with informations organized like that :

```julia
  "ID3" => Any["Heating", SubString{String}["Cold"]] 
 "ID4" => Any["Elec", SubString{String}["Heating", "Cold"]]

```

The function “has” only look in the first part of the values. It never look at the second part where we have the SubString. I use SubString because the two parts of the values represents 2 different things, like IN = Heating and OUT = Cold. When I have 2 outputs, I wrote this using the symbol & and to get the 2 values I used the split function. That’s why I found myself with this type of SubString. I’ve tried many options but it still doens’t look at the SubString part.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [April 30, 2018, 10:55am UTC](https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578/10 "2018-04-30T10:55:46Z")

</div>

Right, then just recursively call `has`:

```julia
julia> d = Dict( "dinner 1" => ["apple", ["carrot", "beans"]], "dinner 2" => "rice", "dinner 3" => "carrot")
Dict{String,Any} with 3 entries:
  "dinner 2" => "rice"
  "dinner 1" => Any["apple", ["carrot", "beans"]]
  "dinner 3" => "carrot"

julia> has(x::AbstractString, y) = x == y
has (generic function with 1 methods)

julia> has(x::AbstractArray, y) = any(has(i, y) for i in x)
has (generic function with 2 methods)

julia> key_value = [k for (k,v) in d if has(v, "carrot")]
2-element Array{String,1}:
 "dinner 1"
 "dinner 3"

```

---

<div class="post-metadata">

**Author:** ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)\
**Post date:** [April 30, 2018, 1:10pm UTC](https://discourse.julialang.org/t/finding-the-set-of-keys-with-the-same-value-in-a-dictionnary/10578/11 "2018-04-30T13:10:40Z")

</div>

Thanks a lot for your incredible help! Problem solved 😀😀😀
