In [1]: list_key = ["M", "L", "XL", "XXL"].index
In [3]: idx = {s: i for i, s in enumerate(["M", "L", "XL", "XXL"])}
In [4]: dict_key = idx.get
In [5]: %timeit list_key("XXL")
41.4 ns ± 0.0398 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
In [6]: %timeit dict_key("XXL")
28.6 ns ± 0.0696 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
In [7]: %timeit list_key("M")
22.5 ns ± 0.0124 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
In [8]: %timeit dict_key("M")
27.6 ns ± 0.0402 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
In [9]: %timeit sorted(['L', 'M', 'XL', 'XXL'], key=list_key)
226 ns ± 0.687 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [10]: %timeit sorted(['L', 'M', 'XL', 'XXL'], key=dict_key)
185 ns ± 0.046 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)