You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
</span></code></pre><p>In the above example, <code>indent</code> is used for pretty printing. Here's how the file looks like:<preclass=language-rubydata-lang=rubystyle=background-color:#f5f5f5;color:#1f1f1f;><codeclass=language-rubydata-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:<preclass=language-pythondata-lang=pythonstyle=background-color:#f5f5f5;color:#1f1f1f;><codeclass=language-pythondata-lang=python><span>$ cat marks.json
</span></code></pre><p>And here's an example of loading a JSON file:<preclass=language-pythondata-lang=pythonstyle=background-color:#f5f5f5;color:#1f1f1f;><codeclass=language-pythondata-lang=python><spanstyle=color:#72ab00;>>>> with </span><spanstyle=color:#b39f04;>open</span><span>(</span><spanstyle=color:#d07711;>'marks.json'</span><span>) </span><spanstyle=color:#72ab00;>as </span><span>f:
</span></code></pre><p><imgalt=infosrc=/images/info.svg> See <ahref=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>:<palign=center><iframeallow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreenframeborder=0height=315loading=lazysrc=https://www.youtube.com/embed/Fd28UTqcU3kwidth=560></iframe></p><br><p><imgalt=infosrc=/images/info.svg> See also my <ahref=https://github.com/learnbyexample/100_page_python_intro>100 Page Python Intro</a> ebook.</div><divclass=post-footer><divclass=post-tags><ahref=https://learnbyexample.github.io/tags/python/>#python</a><ahref=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hrcolor=#e6e6e6><divclass=post-nav><p><aclass=nexthref=https://learnbyexample.github.io/tips/python-tip-19/>Python tip 19: manipulating string case →</a><br></div><hrcolor=#e6e6e6><p>📰 Use <ahref=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <ahref=https://twitter.com/learn_byexample>Twitter</a>, <ahref=https://github.com/learnbyexample>GitHub</a> and <ahref=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <ahref=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hrcolor=#e6e6e6></div></article></div></main></div><scriptsrc=https://learnbyexample.github.io/even.js></script>
18
+
</span></code></pre><p><imgalt=infosrc=/images/info.svg> See <ahref=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>:<palign=center><iframeallow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreenframeborder=0height=315loading=lazysrc=https://www.youtube.com/embed/Fd28UTqcU3kwidth=560></iframe></p><br><p><imgalt=infosrc=/images/info.svg> See also my <ahref=https://github.com/learnbyexample/100_page_python_intro>100 Page Python Intro</a> ebook.</div><divclass=post-footer><divclass=post-tags><ahref=https://learnbyexample.github.io/tags/python/>#python</a><ahref=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hrcolor=#e6e6e6><divclass=post-nav><p><aclass=previoushref=https://learnbyexample.github.io/tips/python-tip-21/>← Python tip 21: sorting iterables based on a key</a><br><p><aclass=nexthref=https://learnbyexample.github.io/tips/python-tip-19/>Python tip 19: manipulating string case →</a><br></div><hrcolor=#e6e6e6><p>📰 Use <ahref=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <ahref=https://twitter.com/learn_byexample>Twitter</a>, <ahref=https://github.com/learnbyexample>GitHub</a> and <ahref=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <ahref=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hrcolor=#e6e6e6></div></article></div></main></div><scriptsrc=https://learnbyexample.github.io/even.js></script>
<!doctype html><htmllang=en><head><metacontent="IE=edge" http-equiv=X-UA-Compatible><metacontent="text/html; charset=utf-8" http-equiv=content-type><metacontent="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>Python tip 21: sorting iterables based on a key</title><linkhref=https://learnbyexample.github.io/atom.xmlrel=alternatetitle=RSStype=application/atom+xml><scriptsrc=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><linkhref=https://learnbyexample.github.io/site.cssrel=stylesheet><metacontent=learnbyexampleproperty=og:title><metacontent=websiteproperty=og:type><metacontent="Learn Python, Regex, Linux, Scripting, Vim, Ebooks, Self-Publishing and Interesting Tech Nuggets." property=og:description><metacontent=https://learnbyexample.github.ioproperty=og:url><metacontent=https://learnbyexample.github.io/images/learnbyexample.pngproperty=og:image><metacontent=1280property=og:image:width><metacontent=640property=og:image:height><metacontent=summary_large_imageproperty=twitter:card><metacontent=@learn_byexampleproperty=twitter:site><linkhref=https://learnbyexample.github.io/favicon.svgrel=icon><linkrel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><divclass=container><divclass=mobile-navbarid=mobile-navbar><divclass=mobile-header-logo><aclass=logohref=/>learnbyexample</a></div><divclass="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><navclass="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ulclass=mobile-menu-list><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/books> Books </a><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/mini> Mini </a><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/tips> Tips </a><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/tags> Tags </a><liclass=mobile-menu-item><ahref=https://learnbyexample.github.io/about> About </a></ul></nav><headerid=header><divclass=logo><ahref=https://learnbyexample.github.io>learnbyexample</a></div><navclass=menu><ul><li><ahref=https://learnbyexample.github.io/books> Books </a><li><ahref=https://learnbyexample.github.io/mini> Mini </a><li><ahref=https://learnbyexample.github.io/tips> Tips </a><li><ahref=https://learnbyexample.github.io/tags> Tags </a><li><ahref=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><divclass=contentid=mobile-panel><articleclass=post><headerclass=post__header><h1class=post__title><ahref=https://learnbyexample.github.io/tips/python-tip-21/>Python tip 21: sorting iterables based on a key</a></h1><divclass=post__meta><spanclass=post__time>2022-12-28</span></div></header><divclass=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:<preclass=language-pythondata-lang=pythonstyle=background-color:#f5f5f5;color:#1f1f1f;><codeclass=language-pythondata-lang=python><spanstyle=color:#7f8989;># based on the absolute value of an element
2
+
</span><spanstyle=color:#7f8989;># note that the input order is maintained for all three values of "4"
</span></code></pre><p>Here are some examples using <ahref=https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions>lambda expressions</a>:<preclass=language-pythondata-lang=pythonstyle=background-color:#f5f5f5;color:#1f1f1f;><codeclass=language-pythondata-lang=python><spanstyle=color:#7f8989;># sorting dictionaries based on values
</span></code></pre><p><imgalt=infosrc=/images/info.svg> See also <ahref=https://docs.python.org/3/howto/sorting.html>docs.python HOWTOs: Sorting</a>.<p><strong>Video demo</strong>:<palign=center><iframeallow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreenframeborder=0height=315loading=lazysrc=https://www.youtube.com/embed/ERWykO67GTUwidth=560></iframe></p><br><p><imgalt=infosrc=/images/info.svg> See also my <ahref=https://github.com/learnbyexample/100_page_python_intro>100 Page Python Intro</a> ebook.</div><divclass=post-footer><divclass=post-tags><ahref=https://learnbyexample.github.io/tags/python/>#python</a><ahref=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hrcolor=#e6e6e6><divclass=post-nav><p><aclass=nexthref=https://learnbyexample.github.io/tips/python-tip-20/>Python tip 20: saving and loading json →</a><br></div><hrcolor=#e6e6e6><p>📰 Use <ahref=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <ahref=https://twitter.com/learn_byexample>Twitter</a>, <ahref=https://github.com/learnbyexample>GitHub</a> and <ahref=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <ahref=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hrcolor=#e6e6e6></div></article></div></main></div><scriptsrc=https://learnbyexample.github.io/even.js></script>
0 commit comments