{"id":4836,"date":"2020-09-15T13:54:44","date_gmt":"2020-09-15T08:24:44","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4836"},"modified":"2020-09-15T14:26:17","modified_gmt":"2020-09-15T08:56:17","slug":"python-map-function-with-examples","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-map-function-with-examples\/","title":{"rendered":"Python map() function with EXAMPLES"},"content":{"rendered":"\n<p>Sometimes you have a list of inputs that need to be passed to a single function. The Python map function helps us do this task. For example, there is a list of numbers like [ 2, 3, 4, 6] and you want to find the square of all the numbers. There are two approaches to this. One is you iterate over the list and pass the number one by one to the function and then save the output one by one in the result list. And the other way is to use the map function. Let\u2019s take a look at code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [2, 3, 4, 6]\n\ndef square(x):\nreturn x * x\nprint(map(square, numbers))\nprint(list(map(square, numbers)))<\/pre>\n\n\n\n<p>The following is the output.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/Hpjubqh4gkUV9uU_lCmQcloLEteVudPOkBIxhPEqCYBpqg1VFyzHlNe3A1kZMWbl7Z_nAmqDJLyJeS5oqprX2W-Fh9FwNSaCp0KbNvQojjXXoNwyjYuPrln25s77ZJvtIwZzan03FouK0m0Tqw\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Using map function with built-in functions<\/h2>\n\n\n\n<p>In the above example, we created our own square functions. Now let\u2019s take a look at how Python map function works with <a href=\"https:\/\/docs.python.org\/3\/library\/functions.html\" rel=\"nofollow noopener\" target=\"_blank\">built-in functions<\/a>. Let\u2019s take the factorial of all the numbers in a list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import math\nnumbers = [2, 3, 4, 6]\nprint(map(math.factorial, numbers))\nprint(list(map(math.factorial, numbers)))<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/-KQiVFaaUdCpND-nOqqu3_ZGcnZ3eTn5yO5uvcaGD4Fl5KFas51gNBvBgC-69a-5lk-dCfoAdRjxZH8t-A-z7bsry2H3EF1AgGYJVWuVFavM-3XTD8i6PS9jlb6qA5aIQyNiHLlm-kJ73-2Aog\" alt=\"map function\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Using map() with Tuple<\/h2>\n\n\n\n<p>Let us apply our map function on <a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-is-python-tuple\/\">tuples<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def UpperCase(n):\nreturn n.upper()\nName = ('john','brick')\nupdated_tuple = map(UpperCase, Name)\nprint(updated_tuple)\nprint(list(updated_tuple))<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/M11_5XrHWy6p2M333Hx1U2csS0FqLAn0x_kBhz5ymNRcdG_IZVaLuOzfFRI-J1Md7p6TzAh5-9i6Aoaqvvijat62dkimqxFwdQ3YysD7hKO6Xs9dtwHW67btJACwDMHBWaOyF48iOEj9Pk1Aaw\" alt=\"map function on tuples\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Using map() with Lambda function<\/h2>\n\n\n\n<p>Let\u2019s use the lambda function inside the map. These programming practices make it really easy to code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [2, 3, 4, 6]\nsquare = map(lambda x: x * 2, numbers)\nprint(square)\nprint(list(square))<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/-V_ymq6JoVwINyhqfzm_Gm8VZf84pHn2536WnbdeehsOvhok1rKAfjMpApB551en4bUrVKPc6C6gc_8IqCU85hbPZHSv6p9Mx4k-3LcxTF8grZYtkGWmUm6mPhThYb1i6aHINYCHgJQVqcAV_w\" alt=\"lambda function\" title=\"\"><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you have a list of inputs that need to be passed to a single function. The Python map function helps us do this task. For example, there is a list of numbers like [ 2, 3, 4, 6] and you want to find the square of all the numbers. There are two approaches to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4838,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[1364,1365,1355],"class_list":["post-4836","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-built-in-functions","tag-map-function-on-tuples","tag-python-map-function"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4836"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4836\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/4838"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}