{"id":9668,"date":"2021-09-01T17:49:39","date_gmt":"2021-09-01T17:49:39","guid":{"rendered":"https:\/\/linuxways.net\/?p=9668"},"modified":"2021-09-01T17:49:39","modified_gmt":"2021-09-01T17:49:39","slug":"pythons-map-function","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/pythons-map-function\/","title":{"rendered":"Python&#8217;s map() Function"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>The map() function in Python is used to return the result as a map object without using an explicit for loop. It can handle iteration without a loop.<\/p>\n<p>This is a useful function that allows you to apply a transformation function to each item of a given iterable. Below is the guide on how to use the map() function in Python as we go through below.<\/p>\n<h2>Example<\/h2>\n<p>We have some command lines:<\/p>\n<pre>def func(n):\r\n\r\nreturn len(n)\r\n\r\nx = map(func, ('cat', 'dog', 'tiger'))\r\n\r\nprint(x)\r\n\r\nprint(list(x))\r\n<\/pre>\n<p>Output:<\/p>\n<p><strong>&lt;map object at 0x00000000020E7940&gt;<\/strong><\/p>\n<p><strong>[3, 3, 5]<\/strong><\/p>\n<h2>Definition<\/h2>\n<p>The map() function executes a specified function for each item in an iterable file. Item in function as a parameter.<\/p>\n<h2>The syntax<\/h2>\n<pre>map (function, iterables)<\/pre>\n<p><strong>Parameter Values<\/strong>:<\/p>\n<p>function: The function executes for each element in the iterable<\/p>\n<p>iterables: a list, tuple, dictionary&#8230; want to browse<\/p>\n<h2>More examples<\/h2>\n<p><strong>Example 1<\/strong>: Double the variable value n:<\/p>\n<pre>def calc(n):\r\n\r\n#Double n\r\n\r\nreturn n + n\r\n\r\nnumbers = (2, 4, 6, 8)\r\n\r\nresult = map(calc, numbers)\r\n\r\n#Convert map object to list\r\n\r\nprint(list(result))<\/pre>\n<p>Output:<\/p>\n<p><strong>[4, 8, 12, 16]<\/strong><\/p>\n<p><strong>Example 2<\/strong>: Using lambda function with map():<\/p>\n<pre>numbers = (2, 4, 6, 8)\r\n\r\nresult = map(lambda n: n+n, numbers)\r\n\r\nprint(list(result))<\/pre>\n<p>Output:<\/p>\n<p><strong>[4, 8, 12, 16]<\/strong><\/p>\n<p><strong>Example 3<\/strong>: Passing multiple iterator parameters to map() using lambda:<\/p>\n<pre>num1 = [2, 3, 4]\r\n\r\nnum2 = [4, 5, 6]\r\n\r\nresult = map(lambda x, y: x + y, num1, num2)\r\n\r\nprint(list(result))<\/pre>\n<p>Output:<\/p>\n<p><strong>[6, 8, 10]<\/strong><\/p>\n<h2>Conclusion<\/h2>\n<p>You have just seen detailed instructions on how to use the map() function in Python.<\/p>\n<p>Thank you for reading.<\/p>","protected":false},"excerpt":{"rendered":"<p>Introduction The map() function in Python is used to return the result as a map object without using an explicit for loop. It can handle iteration without a&hellip;<\/p>","protected":false},"author":1,"featured_media":9763,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[10],"class_list":["post-9668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-python"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=9668"}],"version-history":[{"count":1,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9668\/revisions"}],"predecessor-version":[{"id":9669,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9668\/revisions\/9669"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/9763"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=9668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=9668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=9668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}