Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

Python

Sorting Dictionary by value

from operator import itemgetter
for state, j in sorted(Area.items(), key=itemgetter(1), reverse=True):
https://snipt.net/embed/57f1c8ba4cfc7c26a7d9f93f01ce1621/
https://snipt.net/raw/57f1c8ba4cfc7c26a7d9f93f01ce1621/
57f1c8ba4cfc7c26a7d9f93f01ce1621
python
Python
3
2016-11-17T18:20:31
True
False
False
/api/public/snipt/5674/
sorting-dictionary-by-value
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="n">itemgetter</span> </span><span id="L-2"><a name="L-2"></a><span class="k">for</span> <span class="n">state</span><span class="p">,</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">Area</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span> </span></pre></div> </td></tr></table>
"sort by values", dictionary, python
Copyrighted, illegal, or inappropriate content? Email [email protected].