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-09-16T08:03:09
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 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

Python

Pass lists to dict()

1
2
3
4
5
6
>>> i
['a', 'b']
>>> l
[1, 2]
>>> dict([i,l])
{'a': 'b', 1: 2}
https://snipt.net/embed/1adca8ff7fa6bc60b1885a58c1202562/
https://snipt.net/raw/1adca8ff7fa6bc60b1885a58c1202562/
1adca8ff7fa6bc60b1885a58c1202562
python
Python
7
2016-09-16T04:15:08
True
False
False
/api/public/snipt/3960/
pass-lists-to-dict
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="o">&gt;&gt;&gt;</span> <span class="n">i</span> </span><span id="L-2"><a name="L-2"></a><span class="p">[</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">]</span> </span><span id="L-3"><a name="L-3"></a><span class="o">&gt;&gt;&gt;</span> <span class="n">l</span> </span><span id="L-4"><a name="L-4"></a><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span> </span><span id="L-5"><a name="L-5"></a><span class="o">&gt;&gt;&gt;</span> <span class="nb">dict</span><span class="p">([</span><span class="n">i</span><span class="p">,</span><span class="n">l</span><span class="p">])</span> </span><span id="L-6"><a name="L-6"></a><span class="p">{</span><span class="s">&#39;a&#39;</span><span class="p">:</span> <span class="s">&#39;b&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span> </span></pre></div> </td></tr></table>
dictionary, list, python
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.