Get key for value in dictionary

Hello.
I created a dictionary in order to associate pairs of data.
now i can in this dictionary get a value when i know the key,
but how can i get a key knowing a value ?

Get a list of values ​​from the dictionary. Check the index of the value that interests you. Download the dictionary key list, download the list item with the index previously downloaded.

thak you Patryk_F i thought there was a simplest way do it...

There is:

image

oh yes thank !
i'm just surprise this way is not in dictonary block's !!
but ok for me.

Partially true.

why did you not use the block I showed from the control pallette ?

There can be a lot of ways to do a particular thing.
However, a break block is needed to stop the loop when you get the key.

@vknow360 took in blocks what I wrote about. As you can see it is not difficult at all. 2 more blocks than a loop, and you have a universal return procedure. You can also throw away the variable "key".

Yes, we have not really handled the situation where the value appears more than once :wink: (you either get the first or the last)

Yes, that's true.
But most likely is to get first key as according to docs Index In List returns index of first occurrence.

One issue with the list-based approach is that if the value isn't in the dictionary, you'll get an index out of bounds error. It would be fairly trivial to add a block that does this logic internally. Unfortunately, there is no built-in operation on the internal data structure (Java HashMap) to do this, so we'll have to implement it. The computational complexity will be O(n), so it will be slower than the key lookup.

An alternative design might be to return a list of all keys matching the value, with an empty list being returned in the event no entry contains the value.

well... i didn't think that this was really a problem ! :thinking:

you talk about difficulty dur to several possibilities for the same value...but then in a dictionary each key must be unique ?

effectively differents ways to do this, i just thought that it could exist the same instruction to find a key from a value than to find a value from a key...it seems not...

If each value is unique then there is less of a problem. But if the values are duplicating, you must return the key list.

Here is a different way to package this, same problems with duplicate values ...

So you could avoid having to code like


by setting one to the inverse of the other at run time.

Wouhaou ! how do you have chess pictures in text ??

Give thanks to Unicode

but how do you use it ? is ther a tutorial for it ?

You search the web for 'Unicode white pawn', and copy and paste into the text block.

An international tool site like AI2 eventually forces you into learning some code page information, starting with UTF8.

Google is my teacher.

ok i will have a look on it. thanks