{"id":294,"date":"2024-03-14T19:10:15","date_gmt":"2024-03-14T19:10:15","guid":{"rendered":"https:\/\/offensivepython.com\/?p=294"},"modified":"2024-03-15T01:53:55","modified_gmt":"2024-03-15T01:53:55","slug":"the-map-function","status":"publish","type":"post","link":"https:\/\/offensivepython.com\/index.php\/2024\/03\/14\/the-map-function\/","title":{"rendered":"The Map Function"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"294\" class=\"elementor elementor-294\">\n\t\t\t\t<div class=\"elementor-element elementor-element-31c0a8a6 e-flex e-con-boxed e-con e-parent\" data-id=\"31c0a8a6\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-43baa772 elementor-widget elementor-widget-text-editor\" data-id=\"43baa772\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\n<p>\u00a0 \u00a0 \u00a0You have a list of strings that are numbers and you need to get a sumation of those numbers. How can you do that in Python?\u00a0 Below, I show you three different ways.<\/p>\n<p><span style=\"text-decoration: underline;\">Example Code:<\/span><\/p>\n<p><strong>nums = [&#8220;3&#8221;, &#8220;8&#8221;, &#8220;10&#8221;, &#8220;1&#8221;]<\/strong><\/p>\n<p># This will not work because the list items are strings, not integers or floats<br \/># print(sum(nums))<\/p>\n<p># A traditional way of handling this problem<br \/><strong>total = 0<\/strong><br \/><strong>for num in nums:<\/strong><br \/><strong>\u00a0 \u00a0 total += int(num)<\/strong><br \/><strong>\u00a0 \u00a0 print(&#8220;For loop method:&#8221;, total)<\/strong><\/p>\n<p># A cool Pythonic way of handling the problem is to use a List comprehension<br \/><strong>print(&#8220;List comprehension method: &#8220;, sum([int(num) for num in nums]))<\/strong><\/p>\n<p># Using the map function is probably the best way to handle this problem and also very Pythonic<br \/><strong>print(&#8220;Map method:&#8221;, sum(map(int, nums)))<\/strong><\/p>\n<p><span style=\"text-decoration: underline;\">Output:<\/span><\/p>\n<p>For loop method: 22<br \/>List comprehension method: 22<br \/>Map method: 22<\/p>\n<p>\u00a0 \u00a0 Remember map next time you need to apply a function to each element that is part of an iterable. The syntax is <strong>map(function_to_call, iterable)<\/strong>. In our example, we called the int function for elements belonging to a list. The result was a map object that contained integer elements, which we could then use as an argument for the sum function.<\/p>\n\n<p>\u00a0<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 \u00a0You have a list of strings that are numbers and you need to get a sumation of those numbers. How can you do that in Python?\u00a0 Below, I show you three different ways. Example Code: nums = [&#8220;3&#8221;, &#8220;8&#8221;, &#8220;10&#8221;, &#8220;1&#8221;] # This will not work because the list items are strings, not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[13],"tags":[18,17],"class_list":["post-294","post","type-post","status-publish","format-standard","hentry","category-general","tag-list-comprehension","tag-map"],"_links":{"self":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/comments?post=294"}],"version-history":[{"count":16,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/294\/revisions"}],"predecessor-version":[{"id":331,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/294\/revisions\/331"}],"wp:attachment":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/media?parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/categories?post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/tags?post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}