Skip to content

Commit f1f2fc5

Browse files
added 21st Python tip
1 parent a0a8bac commit f1f2fc5

File tree

9 files changed

+85
-17
lines changed

9 files changed

+85
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ I'm addicted to reading fantasy/sci-fi books, so I have a [blog](https://learnby
6262

6363
## Tips
6464

65+
* [Python tip 21: sorting iterables based on a key](https://learnbyexample.github.io/tips/python-tip-21/)
6566
* [Vim tip 19: working with buffers](https://learnbyexample.github.io/tips/vim-tip-19/)
6667
* [CLI tip 20: expand and unexpand](https://learnbyexample.github.io/tips/cli-tip-20/)
6768
* [Python tip 20: saving and loading json](https://learnbyexample.github.io/tips/python-tip-20/)

atom.xml

Lines changed: 46 additions & 6 deletions
Large diffs are not rendered by default.

sitemap.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@
402402
</url>
403403
<url>
404404
<loc>https://learnbyexample.github.io/tips/</loc>
405-
<lastmod>2022-12-20</lastmod>
405+
<lastmod>2022-12-28</lastmod>
406406
</url>
407407
<url>
408408
<loc>https://learnbyexample.github.io/tips/cli-tip-1/</loc>
@@ -536,6 +536,10 @@
536536
<loc>https://learnbyexample.github.io/tips/python-tip-20/</loc>
537537
<lastmod>2022-12-07</lastmod>
538538
</url>
539+
<url>
540+
<loc>https://learnbyexample.github.io/tips/python-tip-21/</loc>
541+
<lastmod>2022-12-28</lastmod>
542+
</url>
539543
<url>
540544
<loc>https://learnbyexample.github.io/tips/python-tip-3/</loc>
541545
<lastmod>2022-05-16</lastmod>

tags/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tags/python/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tags/tip/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tips/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tips/python-tip-20/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
</span><span style=color:#72ab00;>>>> with </span><span style=color:#b39f04;>open</span><span>(</span><span style=color:#d07711;>'marks.json'</span><span>, </span><span style=color:#d07711;>'w'</span><span>) </span><span style=color:#72ab00;>as </span><span>f:
44
</span><span style=color:#b3933a;>... </span><span>json.</span><span style=color:#5597d6;>dump</span><span>(marks, f, </span><span style=color:#5597d6;>indent</span><span style=color:#72ab00;>=</span><span style=color:#b3933a;>4</span><span>)
55
</span><span style=color:#b3933a;>...
6-
</span></code></pre><p>In the above example, <code>indent</code> is used for pretty printing. Here's how the file looks like:<pre class=language-ruby data-lang=ruby style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-ruby data-lang=ruby><span>$ cat marks.json
6+
</span></code></pre><p>In the above example, <code>indent</code> is used for pretty printing. Here's how the file looks like:<pre class=language-python data-lang=python style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-python data-lang=python><span>$ cat marks.json
77
</span><span>{
8-
</span><span> </span><span style=color:#d07711;>"Rahul"</span><span style=color:#72ab00;>: </span><span style=color:#b3933a;>86</span><span>,
9-
</span><span> </span><span style=color:#d07711;>"Ravi"</span><span style=color:#72ab00;>: </span><span style=color:#b3933a;>92</span><span>,
10-
</span><span> </span><span style=color:#d07711;>"Rohit"</span><span style=color:#72ab00;>: </span><span style=color:#b3933a;>75</span><span>,
11-
</span><span> </span><span style=color:#d07711;>"Rajan"</span><span style=color:#72ab00;>: </span><span style=color:#b3933a;>79
8+
</span><span> </span><span style=color:#d07711;>"Rahul"</span><span>: </span><span style=color:#b3933a;>86</span><span>,
9+
</span><span> </span><span style=color:#d07711;>"Ravi"</span><span>: </span><span style=color:#b3933a;>92</span><span>,
10+
</span><span> </span><span style=color:#d07711;>"Rohit"</span><span>: </span><span style=color:#b3933a;>75</span><span>,
11+
</span><span> </span><span style=color:#d07711;>"Rajan"</span><span>: </span><span style=color:#b3933a;>79
1212
</span><span>}
1313
</span></code></pre><p>And here's an example of loading a JSON file:<pre class=language-python data-lang=python style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-python data-lang=python><span style=color:#72ab00;>>>> with </span><span style=color:#b39f04;>open</span><span>(</span><span style=color:#d07711;>'marks.json'</span><span>) </span><span style=color:#72ab00;>as </span><span>f:
1414
</span><span style=color:#b3933a;>... </span><span>marks </span><span style=color:#72ab00;>= </span><span>json.</span><span style=color:#5597d6;>load</span><span>(f)
1515
</span><span style=color:#b3933a;>...
1616
</span><span style=color:#72ab00;>>>> </span><span>marks
1717
</span><span>{</span><span style=color:#d07711;>'Rahul'</span><span>: </span><span style=color:#b3933a;>86</span><span>, </span><span style=color:#d07711;>'Ravi'</span><span>: </span><span style=color:#b3933a;>92</span><span>, </span><span style=color:#d07711;>'Rohit'</span><span>: </span><span style=color:#b3933a;>75</span><span>, </span><span style=color:#d07711;>'Rajan'</span><span>: </span><span style=color:#b3933a;>79</span><span>}
18-
</span></code></pre><p><img alt=info src=/images/info.svg> See <a href=https://docs.python.org/3/library/json.html>docs.python: json</a> for documentation, more examples, other methods, caveats and so on.<p><strong>Video demo</strong>:<p align=center><iframe allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreen frameborder=0 height=315 loading=lazy src=https://www.youtube.com/embed/Fd28UTqcU3k width=560></iframe></p><br><p><img alt=info src=/images/info.svg> See also my <a href=https://github.com/learnbyexample/100_page_python_intro>100 Page Python Intro</a> ebook.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/python/>#python</a><a href=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=next href=https://learnbyexample.github.io/tips/python-tip-19/>Python tip 19: manipulating string case →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>
18+
</span></code></pre><p><img alt=info src=/images/info.svg> See <a href=https://docs.python.org/3/library/json.html>docs.python: json</a> for documentation, more examples, other methods, caveats and so on.<p><strong>Video demo</strong>:<p align=center><iframe allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreen frameborder=0 height=315 loading=lazy src=https://www.youtube.com/embed/Fd28UTqcU3k width=560></iframe></p><br><p><img alt=info src=/images/info.svg> See also my <a href=https://github.com/learnbyexample/100_page_python_intro>100 Page Python Intro</a> ebook.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/python/>#python</a><a href=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=previous href=https://learnbyexample.github.io/tips/python-tip-21/>← Python tip 21: sorting iterables based on a key</a><br><p><a class=next href=https://learnbyexample.github.io/tips/python-tip-19/>Python tip 19: manipulating string case →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>

tips/python-tip-21/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!doctype html><html lang=en><head><meta content="IE=edge" http-equiv=X-UA-Compatible><meta content="text/html; charset=utf-8" http-equiv=content-type><meta content="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>Python tip 21: sorting iterables based on a key</title><link href=https://learnbyexample.github.io/atom.xml rel=alternate title=RSS type=application/atom+xml><script src=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><link href=https://learnbyexample.github.io/site.css rel=stylesheet><meta content=learnbyexample property=og:title><meta content=website property=og:type><meta content="Learn Python, Regex, Linux, Scripting, Vim, Ebooks, Self-Publishing and Interesting Tech Nuggets." property=og:description><meta content=https://learnbyexample.github.io property=og:url><meta content=https://learnbyexample.github.io/images/learnbyexample.png property=og:image><meta content=1280 property=og:image:width><meta content=640 property=og:image:height><meta content=summary_large_image property=twitter:card><meta content=@learn_byexample property=twitter:site><link href=https://learnbyexample.github.io/favicon.svg rel=icon><link rel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><div class=container><div class=mobile-navbar id=mobile-navbar><div class=mobile-header-logo><a class=logo href=/>learnbyexample</a></div><div class="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><nav class="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ul class=mobile-menu-list><li class=mobile-menu-item><a href=https://learnbyexample.github.io/books> Books </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/mini> Mini </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tips> Tips </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tags> Tags </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/about> About </a></ul></nav><header id=header><div class=logo><a href=https://learnbyexample.github.io>learnbyexample</a></div><nav class=menu><ul><li><a href=https://learnbyexample.github.io/books> Books </a><li><a href=https://learnbyexample.github.io/mini> Mini </a><li><a href=https://learnbyexample.github.io/tips> Tips </a><li><a href=https://learnbyexample.github.io/tags> Tags </a><li><a href=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><div class=content id=mobile-panel><article class=post><header class=post__header><h1 class=post__title><a href=https://learnbyexample.github.io/tips/python-tip-21/>Python tip 21: sorting iterables based on a key</a></h1><div class=post__meta><span class=post__time>2022-12-28</span></div></header><div class=post-content><p>You can use the <code>sort()</code> method for sorting lists inplace. The <code>sorted()</code> function can be used to get a sorted list from any iterable.<p>The <code>key</code> argument accepts the name of a function (i.e. function object) for custom sorting. If two elements are deemed equal based on the result of the function, the original order will be maintained (<em>stable sorting</em>). Here are some examples:<pre class=language-python data-lang=python style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-python data-lang=python><span style=color:#7f8989;># based on the absolute value of an element
2+
</span><span style=color:#7f8989;># note that the input order is maintained for all three values of "4"
3+
</span><span style=color:#72ab00;>>>> </span><span>nums </span><span style=color:#72ab00;>= </span><span>[</span><span style=color:#72ab00;>-</span><span style=color:#b3933a;>1</span><span>, </span><span style=color:#72ab00;>-</span><span style=color:#b3933a;>4</span><span>, </span><span style=color:#b3933a;>309</span><span>, </span><span style=color:#b3933a;>4.0</span><span>, </span><span style=color:#b3933a;>34</span><span>, </span><span style=color:#b3933a;>0.2</span><span>, </span><span style=color:#b3933a;>4</span><span>]
4+
</span><span style=color:#72ab00;>>>> </span><span>nums.</span><span style=color:#5597d6;>sort</span><span>(</span><span style=color:#5597d6;>key</span><span style=color:#72ab00;>=</span><span style=color:#b39f04;>abs</span><span>)
5+
</span><span style=color:#72ab00;>>>> </span><span>nums
6+
</span><span>[</span><span style=color:#b3933a;>0.2</span><span>, </span><span style=color:#72ab00;>-</span><span style=color:#b3933a;>1</span><span>, </span><span style=color:#72ab00;>-</span><span style=color:#b3933a;>4</span><span>, </span><span style=color:#b3933a;>4.0</span><span>, </span><span style=color:#b3933a;>4</span><span>, </span><span style=color:#b3933a;>34</span><span>, </span><span style=color:#b3933a;>309</span><span>]
7+
</span><span>
8+
</span><span style=color:#7f8989;># based on the length of an element
9+
</span><span style=color:#72ab00;>>>> </span><span>words </span><span style=color:#72ab00;>= </span><span>(</span><span style=color:#d07711;>'morello'</span><span>, </span><span style=color:#d07711;>'irk'</span><span>, </span><span style=color:#d07711;>'fuliginous'</span><span>, </span><span style=color:#d07711;>'crusado'</span><span>, </span><span style=color:#d07711;>'seam'</span><span>)
10+
</span><span style=color:#72ab00;>>>> </span><span style=color:#b39f04;>sorted</span><span>(words, </span><span style=color:#5597d6;>key</span><span style=color:#72ab00;>=</span><span style=color:#b39f04;>len</span><span>, </span><span style=color:#5597d6;>reverse</span><span style=color:#72ab00;>=</span><span style=color:#b3933a;>True</span><span>)
11+
</span><span>[</span><span style=color:#d07711;>'fuliginous'</span><span>, </span><span style=color:#d07711;>'morello'</span><span>, </span><span style=color:#d07711;>'crusado'</span><span>, </span><span style=color:#d07711;>'seam'</span><span>, </span><span style=color:#d07711;>'irk'</span><span>]
12+
</span></code></pre><p>Here are some examples using <a href=https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions>lambda expressions</a>:<pre class=language-python data-lang=python style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-python data-lang=python><span style=color:#7f8989;># sorting dictionaries based on values
13+
</span><span style=color:#72ab00;>>>> </span><span>vehicles </span><span style=color:#72ab00;>= </span><span>{</span><span style=color:#d07711;>'bus'</span><span>: </span><span style=color:#b3933a;>10</span><span>, </span><span style=color:#d07711;>'car'</span><span>: </span><span style=color:#b3933a;>20</span><span>, </span><span style=color:#d07711;>'jeep'</span><span>: </span><span style=color:#b3933a;>3</span><span>, </span><span style=color:#d07711;>'cycle'</span><span>: </span><span style=color:#b3933a;>5</span><span>}
14+
</span><span style=color:#72ab00;>>>> </span><span style=color:#b39f04;>sorted</span><span>(vehicles, </span><span style=color:#5597d6;>key</span><span style=color:#72ab00;>=lambda </span><span style=color:#5597d6;>k</span><span>: vehicles[k])
15+
</span><span>[</span><span style=color:#d07711;>'jeep'</span><span>, </span><span style=color:#d07711;>'cycle'</span><span>, </span><span style=color:#d07711;>'bus'</span><span>, </span><span style=color:#d07711;>'car'</span><span>]
16+
</span><span style=color:#72ab00;>>>> </span><span style=color:#a2a001;>dict</span><span>(</span><span style=color:#b39f04;>sorted</span><span>(vehicles.</span><span style=color:#5597d6;>items</span><span>(), </span><span style=color:#5597d6;>key</span><span style=color:#72ab00;>=lambda </span><span style=color:#5597d6;>t</span><span>: t[</span><span style=color:#b3933a;>1</span><span>]))
17+
</span><span>{</span><span style=color:#d07711;>'jeep'</span><span>: </span><span style=color:#b3933a;>3</span><span>, </span><span style=color:#d07711;>'cycle'</span><span>: </span><span style=color:#b3933a;>5</span><span>, </span><span style=color:#d07711;>'bus'</span><span>: </span><span style=color:#b3933a;>10</span><span>, </span><span style=color:#d07711;>'car'</span><span>: </span><span style=color:#b3933a;>20</span><span>}
18+
</span><span>
19+
</span><span style=color:#7f8989;># based on file extension
20+
</span><span style=color:#72ab00;>>>> </span><span>files </span><span style=color:#72ab00;>= </span><span>(</span><span style=color:#d07711;>'report.txt'</span><span>, </span><span style=color:#d07711;>'hello.py'</span><span>, </span><span style=color:#d07711;>'calc.sh'</span><span>, </span><span style=color:#d07711;>'tictactoe.py'</span><span>)
21+
</span><span style=color:#72ab00;>>>> </span><span style=color:#b39f04;>sorted</span><span>(files, </span><span style=color:#5597d6;>key</span><span style=color:#72ab00;>=lambda </span><span style=color:#5597d6;>f</span><span>: f.</span><span style=color:#5597d6;>rsplit</span><span>(</span><span style=color:#d07711;>'.'</span><span>, </span><span style=color:#b3933a;>1</span><span>)[</span><span style=color:#72ab00;>-</span><span style=color:#b3933a;>1</span><span>])
22+
</span><span>[</span><span style=color:#d07711;>'hello.py'</span><span>, </span><span style=color:#d07711;>'tictactoe.py'</span><span>, </span><span style=color:#d07711;>'calc.sh'</span><span>, </span><span style=color:#d07711;>'report.txt'</span><span>]
23+
</span></code></pre><p><img alt=info src=/images/info.svg> See also <a href=https://docs.python.org/3/howto/sorting.html>docs.python HOWTOs: Sorting</a>.<p><strong>Video demo</strong>:<p align=center><iframe allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreen frameborder=0 height=315 loading=lazy src=https://www.youtube.com/embed/ERWykO67GTU width=560></iframe></p><br><p><img alt=info src=/images/info.svg> See also my <a href=https://github.com/learnbyexample/100_page_python_intro>100 Page Python Intro</a> ebook.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/python/>#python</a><a href=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=next href=https://learnbyexample.github.io/tips/python-tip-20/>Python tip 20: saving and loading json →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>

0 commit comments

Comments
 (0)