EDIT:
As requested, here is the problem I am trying to solve:
I have files in a directory that do not have extensions. Based on the output of the "file" command I want to assign the corresponding extension. So my dictionary is assigning strings that can be in this output, to the extensions (eg "ASCII": "txt") but can't know if it will be the exact output. For example :

```
$ file my_file
myfile: ASCII text
# I want to change the extension of myfile to myfile.txt

```

That is what the code I wrote was designed to do, but maybe there are better solutions

I have a dictionnary of strings with keys that have common strings :

```
MY_D = {
    "first_k": "first_v",
    "sec_k": "sec_v",
    "some_key_name_that_includes_others": "value1",
    "some_other_key": "value2",
    ...
    "last_k": "last_v"
    }

```

Sometimes, I have to fetch a value, if the search word is within the key. I use this code:

```
key_I_am_looking_for = "some"
value = list(v for k, v in MY_D.items() if value_I_am_looking_for in k)[-1]

```

But when the key I am looking for appears in multiple possible keys, the value I get is not always the same, in this example it can be either "value1" or "value2".
I noticed the list returned, is not always ordered the same way.

Is there a way I can make this return always the value that corresponds to the longest key matched (here would be "value1")?