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

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:

rdict = Dict(v => k for (k,v) in dict)
1 Like

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

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

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

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

(I am lost for words.)

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

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

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.