{"id":113739,"date":"2020-06-03T08:16:40","date_gmt":"2020-06-03T08:16:40","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=9199"},"modified":"2020-06-03T08:16:40","modified_gmt":"2020-06-03T08:16:40","slug":"and-or-not-how-to-apply-logical-operators-to-all-list-elements-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/06\/03\/and-or-not-how-to-apply-logical-operators-to-all-list-elements-in-python\/","title":{"rendered":"{AND, OR, NOT} How to Apply Logical Operators to All List Elements in Python?"},"content":{"rendered":"<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"{AND, OR, NOT} How to Apply Logical Operators to All List Elements in Python?\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/sb1A4Qf5nSk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p><strong>Problem<\/strong>: Given a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\">list <\/a>of Boolean elements. What&#8217;s the best way to <a href=\"https:\/\/blog.finxter.com\/python-join-list\/\" target=\"_blank\" rel=\"noreferrer noopener\">join<\/a> all elements using the logical &#8220;OR&#8221; and logical &#8220;AND&#8221; operations?<\/p>\n<p><strong>Example<\/strong>: Convert the list <code>[True, True, False]<\/code> using <\/p>\n<ul>\n<li>The logical &#8220;AND&#8221; operation to <code>True and True and False = False<\/code>,<\/li>\n<li>The logical &#8220;OR&#8221; operation to <code>True or True or False = True<\/code>, and<\/li>\n<li>The logical &#8220;NOT&#8221; operation to <code>[not True, not True, not False] = [False, False, True]<\/code>. <\/li>\n<\/ul>\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/logicalOpsList-1024x576.jpg\" alt=\"\" class=\"wp-image-9207\" width=\"768\" height=\"432\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/logicalOpsList-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/logicalOpsList-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/logicalOpsList-768x432.jpg 768w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n<p><strong>Solution<\/strong>: <\/p>\n<ul>\n<li>To perform logical &#8220;AND&#8221;, use the built-in Python function <code>all()<\/code>,<\/li>\n<li>To perform logical &#8220;OR&#8221;, use the built-in Python function <code>any()<\/code>, and<\/li>\n<li>To perform logical &#8220;NOT&#8221;, use a list comprehension statement <code>[not x for x in list]<\/code>. <\/li>\n<\/ul>\n<p>Here&#8217;s the solution for our three examples:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lst = [True, True, False] # Logical \"AND\"\nprint(all(lst))\n# False # Logical \"OR\"\nprint(any(lst))\n# True # Logical \"NOT\"\nprint([not x for x in lst])\n# [False, False, True]\n<\/pre>\n<p>This way, you can combine an arbitrary iterable of Booleans into a single Boolean value.<\/p>\n<p><em><strong>Puzzle<\/strong>: Guess the output of this interactive code snippet&#8212;and run it to check if you were correct!<\/em><\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/dd8afa5044\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen=\"\" width=\"100%\" height=\"356\" frameborder=\"0\"><\/iframe> <\/p>\n<p>The challenge in the puzzle is to know that Python comes with <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-check-if-a-python-list-is-empty\/\" target=\"_blank\">implicit Boolean type conversion<\/a>: every object has an associated Boolean value. Per convention, all objects are True except &#8220;empty&#8221; or &#8220;zero&#8221; objects such as <code>[]<\/code>, <code>''<\/code>, <code>0<\/code>, and <code>0.0<\/code>. Thus, the result of the function call <code>all([True, True, 0])<\/code> is <code>False<\/code>.<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a list of Boolean elements. What&#8217;s the best way to join all elements using the logical &#8220;OR&#8221; and logical &#8220;AND&#8221; operations? Example: Convert the list [True, True, False] using The logical &#8220;AND&#8221; operation to True and True and False = False, The logical &#8220;OR&#8221; operation to True or True or False = True, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-113739","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=113739"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/113739\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=113739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=113739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=113739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}