{"id":25163,"date":"2021-12-31T05:26:44","date_gmt":"2021-12-31T05:26:44","guid":{"rendered":"https:\/\/www.askpython.com\/?p=25163"},"modified":"2021-12-31T05:26:45","modified_gmt":"2021-12-31T05:26:45","slug":"one-liners-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/one-liners-in-python","title":{"rendered":"Useful One-Liners In Python &#8211; A Quick Guide"},"content":{"rendered":"\n<p>Python is one of the most powerful and user-friendly programming languages ever created. Python is popular among programmers because it simplifies complicated tasks.<\/p>\n\n\n\n<p>This tutorial will go through some easy and entertaining one-liners. Let&#8217;s get started!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Combine the contents of two dictionaries into a single one.<\/h2>\n\n\n\n<p>If you&#8217;re using <strong>Python3.9 or above<\/strong>, you may use | for this. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nx = {&#039;a&#039;: 11, &#039;b&#039;: 22}\ny = {&#039;c&#039;: 13, &#039;d&#039;: 44}\nz = x | y\nprint(z)\n<\/pre><\/div>\n\n\n<p>The output looks like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n{&#039;a&#039;: 11, &#039;b&#039;: 22, &#039;c&#039;: 13, &#039;d&#039;: 44}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Get the most frequent element<\/h2>\n\n\n\n<p>Let&#8217;s utilize the <strong><code>most_common()<\/code><\/strong> function from the collections module to achieve this.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nfrom collections import Counter\nl = &#x5B;&#039;1&#039;, &#039;b&#039;, &#039;2&#039;, &#039;a&#039;, &#039;3&#039;, &#039;z&#039;, &#039;3&#039;, &#039;a&#039;, &#039;2&#039;, &#039;3&#039;]\nCounter(l).most_common()&#x5B;0]&#x5B;0]\n<\/pre><\/div>\n\n\n<p>The code returns <strong>&#8216;3&#8217;<\/strong> as the output which is correct!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Get quotient and remainder at the same time<\/h2>\n\n\n\n<p><code>divmod()<\/code> returns a tuple and its functionality stems from the fact that it combines modulo percent and division\/operators.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nQ, R = divmod(35632, 5)\nprint(&quot;Quo. - &quot;,Q)\nprint(&quot;Rem. - &quot;,R)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nQuo. -  7126\nRem. -  2\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Find the first n Fibonacci numbers<\/h2>\n\n\n\n<p>This will be an excellent exercise on remembering lambda functions and recursion in Python.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nfib = lambda x: x if x &lt;= 1 else fib(x - 1) + fib(x - 2)\nprint(fib(20))\nprint(fib(5))\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n6765\n5\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Remove duplicate elements from a list<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist(set(&#x5B;&#039;1&#039;, &#039;1&#039;, &#039;2&#039;, &#039;1&#039;, &#039;3&#039;]))\n<\/pre><\/div>\n\n\n<p>In Python, each element in a set is unique, therefore there will be no duplicates.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#039;1&#039;, &#039;3&#039;, &#039;2&#039;]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p id=\"block-d144faf1-1abb-488c-a4bd-8a80520fbf7b\">Congratulations! You just learned 5 useful one-liners in the Python programming language. Hope you enjoyed it! &#x1f607;<\/p>\n\n\n\n<p id=\"block-4e73ecff-f7fa-46a5-be6e-35bbb0867ba8\">Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:<\/p>\n\n\n\n<ol class=\"wp-block-list\" id=\"block-39ec4c2c-ab54-45a6-804e-58eb24573b45\"><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/stock-price-prediction-python\"><\/a><a href=\"https:\/\/www.askpython.com\/python\/tricks-for-easier-debugging-in-python\">Tricks for Easier Debugging in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/best-tips-to-score-well-in-college-programming-assignment\">Best Tips to Score Well in College Programming Assignment<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/matplotlib\/matplotlib-plotting-tips\">3 Matplotlib Plotting Tips to Make Plotting Effective<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/competitive-programming\">Competitive Programming in Python: What you need to know?<\/a><\/li><\/ol>\n\n\n\n<p id=\"block-177e4b87-db13-427b-90fa-d70aac750e1d\">Thank you for taking your time out! Hope you learned something new!! &#x1f604;<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Python is one of the most powerful and user-friendly programming languages ever created. Python is popular among programmers because it simplifies complicated tasks. This tutorial will go through some easy and entertaining one-liners. Let&#8217;s get started! Combine the contents of two dictionaries into a single one. If you&#8217;re using Python3.9 or above, you may use [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":25208,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-25163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/25163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=25163"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/25163\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/25208"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=25163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=25163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=25163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}