{"id":1513,"date":"2022-12-07T20:00:00","date_gmt":"2022-12-07T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=1513"},"modified":"2023-01-03T18:43:19","modified_gmt":"2023-01-03T09:43:19","slug":"map","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/map\/","title":{"rendered":"[Python] How to Use map() Function"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to use map() function<\/span>.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Contents<\/span><\/p><div class=\"free-inbox\">\n<p><\/p>\n\n\n\n<ul><li>What is map function?<\/li><li>Basic usage of map function<\/li><li>Combination of map function and User-Defined function<\/li><li>Combination of map function and lambda function<\/li><li>[Appendix] Combination of map function and sum function<\/li><li>[Appendix] Combination of map function and split<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">What is map function?<\/h2>\n\n\n\n<p>In the beginning, <span class=\"st-mymarker-s\">I explain briefly map() function<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span class=\"st-mymarker-s\">Map() function is built-in function<\/span> in Python.<\/p>\n\n\n\n<p>So, We can use map() function without &#8220;import&#8221;.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>By using map(), function can be applied to all elements of the iterable object( list, tuple, etc.).<\/p>\n\n\n\n<p>This function can be a built-in function, a user-defined function, or a lambda function.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Basic usage of map function<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">basic usage of map() function<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Specify function as the first argument and iterable object (list, tuple, etc.) as the second argument.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\">map( Function, iterable object )<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Map() function returns map object.<\/p>\n\n\n\n<p>The elements cannot be checked as map objects.<\/p>\n\n\n\n<p>The map object is converted to list with list function.<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<p>The abs function is applied to each element of list.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>list_test = &#091;11, 0.3, -5, 3, -1]\nprint(list_test)\n# &#091;11, 0.3, -5, 3, -1]\n\nlist_test_m = map(abs, list_test)\nprint(list_test_m)\n# &lt;map object at 0x00000289948332E8&gt;\nprint(list(list_test_m))\n# &#091;11, 0.3, 5, 3, 1]<\/code><\/pre>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>For reference, here is an example of &#8220;for loop&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>list_test = &#091;11, 0.3, -5, 3, -1]\nprint(list_test)\n# &#091;11, 0.3, -5, 3, -1]\n\nlist_test_abs = &#091;]\nfor num in list_test :\n    list_test_abs.append(abs(num))\n\nprint(list(list_test_abs))\n# &#091;11, 0.3, 5, 3, 1]<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Combination of map function and User-Defined function<\/h2>\n\n\n\n<p>Map() function can be specified <span class=\"st-mymarker-s\">user-defined function as its argument<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>def square(num) :\n    return num*num\n\nlist_test = &#091;11, 0.3, -5, 3, -1]\n\nlist_test_m = map(square, list_test)\nprint(list(list_test_m))\n# &#091;121, 0.09, 25, 9, 1]<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The function &#8220;square&#8221; is defined to square a number using def statement.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Combination of map function and lambda function<\/h2>\n\n\n\n<p>Map() function can be specified <span class=\"st-mymarker-s\">lambda function as its argument<\/span>.<\/p>\n\n\n\n<p>Lambda is useful when you want to use simple calculations.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>list_test = &#091;11, 0.3, -5, 3, -1]\n\nfunc = lambda num : num*num\n\nlist_test_m = map(func, list_test)\nprint(list(list_test_m))\n# &#091;121, 0.09, 25, 9, 1]<\/code><\/pre>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>You can also use lambda in the arguments of map() function.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>list_test = &#091;11, 0.3, -5, 3, -1]\n\nlist_test_m = map(lambda num : num*num, list_test)\nprint(list(list_test_m))\n# &#091;121, 0.09, 25, 9, 1]<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">[Appendix] Combination of map function and sum function<\/h2>\n\n\n\n<p>Here are some other use cases of the map function for reference.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>I show an example of <span class=\"st-mymarker-s\">combination of map function and sum function<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>list_test = &#091;11, 0.3, -5, 3, -1]\n\nresult = sum(map(lambda num : num*num, list_test))\nprint(result)\n# 156.09<\/code><\/pre>\n\n\n\n<p>The squared elements are added with sum() function.<\/p>\n\n\n\n<p>It is not interesting because it is just calculating the sum .<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>You can also detect the number of elements that contain certain character.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits = &#091;\"Apple\", \"Banana\", \"Orange\", \"Mango\", \"Lemon\"]\n\nsearch_char = \"a\"\n\nresult = sum(map(lambda fruit : search_char in fruit, fruits))\nprint(result)\n# 3<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">[Appendix] Combination of map function and split<\/h2>\n\n\n\n<p>I show an example of <span class=\"st-mymarker-s\">combination of map function and split<\/span>.<\/p>\n\n\n\n<p>&#8220;Split&#8221; is a method to split a string.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>By combining the split with the map function, numbers entered as a string can be formatted into list.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>num_str = \"5 10 -2 1\"\n\nnum_list =list(map(int, num_str.split()))\nprint(num_list)\n# &#091;5, 10, -2, 1]<\/code><\/pre>\n\n\n\n<p>The numbers entered as space-delimited string can be formatted into list.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In addition, by using the input function, numbers entered from the keyboard are stored in list.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>input_list =list(map(int, input(\"Please enter a space-separated integer\").split()))\n\nprint(input_list)\n# Please enter a space-separated integer\n>> 11 -2 3\n# &#091;11, -2, 3]<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-matched-content-rows-num=\"4,2\"\n     data-matched-content-columns-num=\"1,4\"\n     data-matched-content-ui-type=\"image_stacked,image_stacked\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"2243394422\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to use map() function. What is map function? In the beginning, I explain brie &#8230; <\/p>\n","protected":false},"author":1,"featured_media":1974,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,9],"tags":[28,16],"_links":{"self":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1513"}],"collection":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/comments?post=1513"}],"version-history":[{"count":100,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1513\/revisions"}],"predecessor-version":[{"id":3213,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1513\/revisions\/3213"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/1974"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=1513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=1513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=1513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}