# Is something like reversed Dict? findall(x -\> x=="house", cc) is to slow:/

**URL:** <https://discourse.julialang.org/t/is-something-like-reversed-dict-findall-x-x-house-cc-is-to-slow/16443>\
**Category:** General Usage\
**Created:** [October 17, 2018, 2:20pm UTC](https://discourse.julialang.org/t/is-something-like-reversed-dict-findall-x-x-house-cc-is-to-slow/16443 "2018-10-17T14:20:45Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [October 20, 2018, 8:43am UTC](https://discourse.julialang.org/t/is-something-like-reversed-dict-findall-x-x-house-cc-is-to-slow/16443/21 "2018-10-20T08:43:17Z")

</div>

I’ve used almost exactly the same construct as @Tamas_Papp in the past (although I didn’t deconstruct the pair – I like your solution better Tamas!). The problem I see with it is that it unnecessarily creates an empty vector, just to immediately add an object to it. It would be better to have a way to add a mapping if it doesn’t exist, or update it if it already exists. For example, in .NET there is `AddOrUpdate(key, addValueFunc, updateValueFunc)` for this purpose.

Also, I would have expected it to be faster than @ExpandingMan’s solution (which does a double lookup), but for some reason it’s not on my machine (they perform about the same). I haven’t analyzed why; if it’s due to what I mentioned above, or some inefficiency in `get!`. If someone knows a more efficient way to construct this type of dictionary, I’d be interested to hear it!

Btw, as for creating a reverse dictionary without duplicates, here’s a more concise and efficient way than what’s previously mentioned in this thread:

```julia
rdict = Dict(v => k for (k,v) in dict)

```

---

<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:** [October 20, 2018, 8:44am UTC](https://discourse.julialang.org/t/is-something-like-reversed-dict-findall-x-x-house-cc-is-to-slow/16443/22 "2018-10-20T08:44:57Z")

</div>

> [@bennedich](#):
>
> The problem I see with it is that it unnecessarily creates an empty vector, just to immediately add an object to it.

Why do you think this is a problem? Did you benchmark this an notice a significant overhead compared to another solution?

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [October 20, 2018, 11:56am UTC](https://discourse.julialang.org/t/is-something-like-reversed-dict-findall-x-x-house-cc-is-to-slow/16443/23 "2018-10-20T11:56:39Z")

</div>

Big Thanks for the lesson of Julia ! My code was longer but for me is more clear 😉

```julia
for i in keys(mydict)
slowo=get(mydict,i,"")
if haskey(replslow,slowo) push!(get(replslow,slowo,""),i) end 
if !haskey(replslow,slowo) get!(replslow,slowo,[i]) end 
end

```

> [@Tamas\_Papp](#):
>
> where {K,V}

What mean ‘where {’ ? Nothin in documantion about it.  
Paul

---

<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:** [October 20, 2018, 12:08pm UTC](https://discourse.julialang.org/t/is-something-like-reversed-dict-findall-x-x-house-cc-is-to-slow/16443/24 "2018-10-20T12:08:48Z")

</div>

> [@programista](#):
>
> What mean ‘where {’ ? Nothin in documantion about it.

[(I am lost for words.)](https://docs.julialang.org/en/stable/manual/methods/#Parametric-Methods-1)

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [October 20, 2018, 6:28pm UTC](https://discourse.julialang.org/t/is-something-like-reversed-dict-findall-x-x-house-cc-is-to-slow/16443/25 "2018-10-20T18:28:13Z")

</div>

I understand that in this case it’s just about defining K and V. Do you?  
Paul

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [October 20, 2018, 7:42pm UTC](https://discourse.julialang.org/t/is-something-like-reversed-dict-findall-x-x-house-cc-is-to-slow/16443/26 "2018-10-20T19:42:05Z")

</div>

> [@Tamas\_Papp](#):
>
> Why do you think this is a problem? Did you benchmark this an notice a significant overhead compared to another solution?

Perhaps “problem” is a bit of an overstatement, but it feels like there’s room for improvement, which irks me 🙂

```julia
julia> @btime (for n = 1:1000; a = Vector{Int64}(); push!(a, 42); end)
55.875 μs (2000 allocations: 125.00 KiB)

julia> @btime (for n = 1:1000; a = [42]; end)
32.211 μs (1000 allocations: 93.75 KiB)

```

It also bothers me that it’s not faster than the double-lookup solution (on my machine). I would expect it to be. In fact, for dictionaries with mostly unique values, the double-lookup solution performs better.

[Previous page](https://discourse.julialang.org/t/is-something-like-reversed-dict-findall-x-x-house-cc-is-to-slow/16443.md?page=1)
