:PROPERTIES:
:ID:       e9adeaaa-701a-4473-a30b-94f1e744c6d1
:mtime:    20220416112424
:ctime:    20220416105003
:END:
#+title: Obtenir la clé d'un dictionnaire pour laquelle la valeur est la plus petite

* Howto
#+BEGIN_SRC python :results output
from random import randint
from timethese import cmpthese, pprint_cmp

data = {randint(0, 1000): randint(0, 1000) for i in range(100)}

def convert_to_lists():
    v=list(data.values())
    k=list(data.keys())
    return k[v.index(min(v))]

def dict_comprehension():
    d3={v:k for k,v in data.items()}
    return d3[min(d3)]

def filter_and_lambda():
    return list(filter(lambda t: t[1]==min(data.values()), data.items()))[0][0]

def filter_and_lambda2():
    m=min(data.values())
    return list(filter(lambda t: t[1]==m, data.items()))[0][0]

def list_comprehension():
    return [k for k,v in data.items() if v==min(data.values())][0]

def list_comprehension2():
    m=min(data.values())
    return [k for k,v in data.items() if v==m][0]

def using_min():
    return min(data, key=data.get)

def convert_to_lists_shortened():
    v=list(data.values())
    return list(data.keys())[v.index(min(v))]

def using_min_and_lambda():
    return min(data, key=lambda k: data[k])

tl = [convert_to_lists,
      dict_comprehension,
      filter_and_lambda,
      filter_and_lambda2,
      list_comprehension,
      list_comprehension2,
      using_min,
      convert_to_lists_shortened,
      using_min_and_lambda]

print(pprint_cmp(cmpthese(1000, tl)))

#+END_SRC

#+RESULTS:
#+begin_example
                                  Rate  0.convert_to_lists  7.convert_to_lists_shortened  6.using_min  5.list_comprehension2  1.dict_comprehension  8.using_min_and_lambda  3.filter_and_lambda2  4.list_comprehension  2.filter_and_lambda
          0.convert_to_lists  424342/s                   .                            1%          52%                   105%                  178%                    216%                  312%                 5414%                5745%
7.convert_to_lists_shortened  419268/s                 -1%                             .          50%                   103%                  175%                    213%                  307%                 5348%                5675%
                 6.using_min  280002/s                -34%                          -33%            .                    36%                   84%                    109%                  172%                 3538%                3757%
       5.list_comprehension2  206598/s                -51%                          -51%         -26%                      .                   36%                     54%                  101%                 2585%                2746%
        1.dict_comprehension  152444/s                -64%                          -64%         -46%                   -26%                     .                     14%                   48%                 1881%                2000%
      8.using_min_and_lambda  134115/s                -68%                          -68%         -52%                   -35%                  -12%                       .                   30%                 1643%                1747%
        3.filter_and_lambda2  103036/s                -76%                          -75%         -63%                   -50%                  -32%                    -23%                     .                 1239%                1319%
        4.list_comprehension    7696/s                -98%                          -98%         -97%                   -96%                  -95%                    -94%                  -93%                     .                   6%
         2.filter_and_lambda    7260/s                -98%                          -98%         -97%                   -96%                  -95%                    -95%                  -93%                   -6%                    .
#+end_example

* Références
 * [[https://docs.python.org/3/library/functions.html#min][Python]]
 * [[https://blog.finxter.com/how-to-get-the-key-with-minimum-value-in-a-python-dictionary/][Finxter]]
 * [[https://stackoverflow.com/questions/268272/getting-key-with-maximum-value-in-dictionary][Stackoverflow]]
