{"id":3922,"date":"2021-01-27T16:20:17","date_gmt":"2021-01-27T16:20:17","guid":{"rendered":"https:\/\/www.gubatron.com\/blog\/?p=3922"},"modified":"2021-01-27T16:28:21","modified_gmt":"2021-01-27T16:28:21","slug":"python-in-functional-style-how-to-add-2-lists-of-integers-without-using-loops","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/python-in-functional-style-how-to-add-2-lists-of-integers-without-using-loops\/","title":{"rendered":"Python in Functional Style: How to add 2 lists of integers without using loops"},"content":{"rendered":"\r\n<p>Usually you&#8217;d add a list of integers this way:<br \/><br \/><\/p>\r\n<p>[pastacode lang=&#8221;python&#8221; manual=&#8221;a%20%3D%20%5B2%2C%202%2C%202%2C%202%5D%0Ab%20%3D%20%5B2%2C%202%2C%202%2C%202%5D%0Ac%20%3D%20%5B%5D%0Afor%20i%20in%20range(len(a))%3A%0A%20c.append(a%5Bi%5D%20%2B%20b%5Bi%5D)&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\r\n<p>You can do it functionally without any loops in different ways:<\/p>\r\n<h4><br \/><br \/>Using map and a lambda that adds them up<\/h4>\r\n<p>[pastacode lang=&#8221;python&#8221; manual=&#8221;c%20%3D%20list(map(lambda%20x%2Cy%3A%20x%2By%2C%20a%2C%20b))&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\r\n<p>or you can import the <strong>add<\/strong> operator as a named function<\/p>\r\n<p>[pastacode lang=&#8221;python&#8221; manual=&#8221;from%20operator%20import%20add%0Ac%20%3D%20list(map(add%2C%20a%2C%20b))&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\r\n<h4><br \/>Ever zipped two lists into a list of tuples?<br \/><br \/><\/h4>\r\n<p>There&#8217;s another more convoluted way if you want to play with &#8220;zip&#8221;.<br \/><br \/><img loading=\"lazy\" decoding=\"async\" class=\"\" src=\"https:\/\/external-content.duckduckgo.com\/iu\/?u=https%3A%2F%2Fae01.alicdn.com%2Fkf%2FHTB1x3GhNFXXXXaGXFXXq6xXFXXXu%2FClose-End-10cm-80cm-5-10pcs-white-Metal-Zipper-for-Sewing-zip-Garment-Accessories-Jeans-Zippers.jpg&amp;f=1&amp;nofb=1\" alt=\"Close End 10cm 80cm 5# 10pcs white Metal Zipper for Sewing ...\" width=\"325\" height=\"325\" \/><br \/>Imagine a jacket zipper and the teeth on each side of the zipper is one element on each one of the list.<br \/><br \/><br \/>When you zip the lists <strong>a<\/strong> and <strong>b<\/strong>, you end up with a list of tuples of matching elements from the given lists.<\/p>\r\n<p>[pastacode lang=&#8221;python&#8221; manual=&#8221;%3E%3E%3E%20list(zip(a%2Cb))%0A%5B(2%2C%202)%2C%20(2%2C%202)%2C%20(2%2C%202)%2C%20(2%2C%202)%5D&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\r\n<p>you could now map a function to add the elements within each tuple on that list. <br \/><br \/><br \/><\/p>\r\n<p>[pastacode lang=&#8221;python&#8221; manual=&#8221;%3E%3E%3E%20list(map(lambda%20tup%3A%20tup%5B0%5D%2Btup%5B1%5D%2C%20zip(a%2Cb)))%0A%5B4%2C%204%2C%204%2C%204%5D&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]<\/p>\r\n<p><br \/>Notice how we don&#8217;t convert to list after zip, we can work directly with the zip iterator, we only convert to list with the final map iterator. <br \/><br \/><strong>Python 2 &amp; 3 Note:<\/strong><br \/><br \/>In Python 2 it&#8217;s not necessary to use <strong>list()<\/strong>, the map() and zip() methods return lists there. But stay away from Python 2, a lot of projects are now discontinuing support.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Usually you&#8217;d add a list of integers this way: [pastacode lang=&#8221;python&#8221; manual=&#8221;a%20%3D%20%5B2%2C%202%2C%202%2C%202%5D%0Ab%20%3D%20%5B2%2C%202%2C%202%2C%202%5D%0Ac%20%3D%20%5B%5D%0Afor%20i%20in%20range(len(a))%3A%0A%20c.append(a%5Bi%5D%20%2B%20b%5Bi%5D)&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/] You can do it functionally without any loops in different ways: Using map and a lambda that adds them up [pastacode lang=&#8221;python&#8221; manual=&#8221;c%20%3D%20list(map(lambda%20x%2Cy%3A%20x%2By%2C%20a%2C%20b))&#8221; message=&#8221;&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/] or you can import the add operator as a named function [pastacode lang=&#8221;python&#8221; manual=&#8221;from%20operator%20import%20add%0Ac%20%3D%20list(map(add%2C%20a%2C%20b))&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[1639,1638,1634,1431,1635,1458,1637,1636],"class_list":["post-3922","post","type-post","status-publish","format-standard","hentry","category-code","tag-fp","tag-functional-programming","tag-iterators","tag-list","tag-map","tag-python","tag-python3","tag-zip"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-11g","jetpack-related-posts":[{"id":2568,"url":"https:\/\/www.gubatron.com\/blog\/bfs-vs-dfs-graph-search-algorithms-in-python\/","url_meta":{"origin":3922,"position":0},"title":"BFS vs DFS Graph Search Algorithms in Python","author":"gubatron","date":"November 15, 2011","format":false,"excerpt":"Here are implementations of iterative BFS and DFS search algorithms in Python. These are just to illustrate the slight difference in implementation of these algorithms. Basically, if you want to go deep, with DFS, you can use a queue on which you'll be adding the next elements to explore as\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3956,"url":"https:\/\/www.gubatron.com\/blog\/accessing-and-manipulating-a-32bit-integer-as-an-array-in-c-thanks-to-union\/","url_meta":{"origin":3922,"position":1},"title":"Accessing and manipulating a 32bit integer as a byte array in C++ using unions","author":"gubatron","date":"November 12, 2021","format":false,"excerpt":"I don't think I've ever used union for anything, but today I came across a very interesting use case to avoid bit-shifting tricks when dealing with data embedded in numbers. What's a union? Microsoft defines it this way A\u00a0union\u00a0is a user-defined type in which all members share the same memory\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3616,"url":"https:\/\/www.gubatron.com\/blog\/codejava-argb_8888-pixel-abstraction\/","url_meta":{"origin":3922,"position":2},"title":"[CODE\/JAVA] ARGB_8888 Pixel Abstraction","author":"gubatron","date":"December 4, 2016","format":false,"excerpt":"This is one of the ways you can decode Pixel colors out of the integers you get from Android Pixels. ARGB_8888, stands for Alpha, Reg, Green, Blue. The 8's stand for the number of bits per channel. In Android, signed int's are used to represent pixel's alpha\/color information. Since Android's\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3656,"url":"https:\/\/www.gubatron.com\/blog\/code-arduino-4-bit-led-counter\/","url_meta":{"origin":3922,"position":3},"title":"[CODE] Arduino 4 BIT LED binary counter","author":"gubatron","date":"January 11, 2017","format":false,"excerpt":"Here's my \"Hello World\" on Arduino. A 4-bit binary counter for arduino. If you add another LED on the 5th pin, then it can count from 0 to 31 (32 values) Circuit wired by @KataySantos [pastacode lang=\"c\" manual=\"int%20counter%20%3D%20-1%3B%0Aint%20MAX_VALUE%20%3D%2016%3B%0A%0Avoid%20updateLED(int%20pin%2C%20int%20bit%2C%20int%20ctr)%20%7B%0A%20%20digitalWrite(pin%2C%20(ctr%20%26%20(1%3C%3Cbit))%20%3D%3D%20(1%3C%3Cbit)%20%3F%20HIGH%20%3A%20LOW)%3B%0A%7D%0Avoid%20setup()%20%7B%0A%20%20%2F%2F%20this%20runs%20once%0A%20%20pinMode(1%2C%20OUTPUT)%3B%0A%20%20pinMode(2%2C%20OUTPUT)%3B%0A%20%20pinMode(3%2C%20OUTPUT)%3B%0A%20%20pinMode(4%2C%20OUTPUT)%3B%0A%7D%0A%0Avoid%20loop()%20%7B%0A%20%20%2F%2F%20this%20runs%20repeatedly%0A%20%20counter%20%3D%20(counter%20%2B%201)%20%25%20MAX_VALUE%3B%0A%20%20updateLED(1%2C%200%2C%20counter)%3B%0A%20%20updateLED(2%2C%201%2C%20counter)%3B%0A%20%20updateLED(3%2C%202%2C%20counter)%3B%0A%20%20updateLED(4%2C%203%2C%20counter)%3B%0A%20%20delay(500)%3B%0A%7D\" message=\"\" highlight=\"\" provider=\"manual\"\/]","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3651,"url":"https:\/\/www.gubatron.com\/blog\/codephp-jpgraph-how-to-output-your-graph-as-a-base64-encoded-image\/","url_meta":{"origin":3922,"position":4},"title":"[CODE\/PHP] JpGraph: How to output your graph as a base64 encoded image","author":"gubatron","date":"January 11, 2017","format":false,"excerpt":"Some times you just want to output the image created by your $graph object without having to create a separate .php script that would need to receive a bunch of parameters. Here's a function you can pass your $graph object right before the $graph->Stroke(); call [pastacode lang=\"php\" manual=\"function%20graphInSrc(%24graph%2C%20%24width%2C%20%24height)%20%7B%0A%20%20%24img%20%3D%20%24graph-%3EStroke(_IMG_HANDLER)%3B%0A%20%20ob_start()%3B%0A%20%20imagepng(%24img)%3B%0A%20%20%24img_data%20%3D%20ob_get_contents()%3B%0A%20%20ob_end_clean()%3B%0A%0A%20%20echo%20'%3Cimg%20width%3D%22'.%24width.'%22%20height%3D%22'.%24height.'%22%20src%3D%22data%3Aimage%2Fpng%3Bbase64%2C'.base64_encode(%24img_data).'%22%2F%3E'%3B%0A%7D\" message=\"\" highlight=\"\"\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3388,"url":"https:\/\/www.gubatron.com\/blog\/how-to-create-a-list-that-holds-different-object-types-using-void-in-c\/","url_meta":{"origin":3922,"position":5},"title":"How to create a list that holds different object types using `void*` in C.","author":"gubatron","date":"April 5, 2015","format":false,"excerpt":"I remember being in school back around 1998 and not knowing enough about C to do this. After coding in other languages, then going back to C++ and understanding at a lower level how references and pointers work, this was pretty easy to figure out. In this exercise I store\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-12-20-at-7.05.10-PM.png?fit=1200%2C438&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-12-20-at-7.05.10-PM.png?fit=1200%2C438&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-12-20-at-7.05.10-PM.png?fit=1200%2C438&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-12-20-at-7.05.10-PM.png?fit=1200%2C438&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-12-20-at-7.05.10-PM.png?fit=1200%2C438&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3922"}],"version-history":[{"count":7,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3922\/revisions"}],"predecessor-version":[{"id":3935,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3922\/revisions\/3935"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}