{"id":771,"date":"2021-01-04T14:00:23","date_gmt":"2021-01-04T11:00:23","guid":{"rendered":"https:\/\/www.etemkeskin.com\/?p=771"},"modified":"2021-01-04T14:38:35","modified_gmt":"2021-01-04T11:38:35","slug":"map-filter-and-reduce-functions-in-python","status":"publish","type":"post","link":"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/04\/map-filter-and-reduce-functions-in-python\/","title":{"rendered":"Map, Filter and Reduce Functions in Python"},"content":{"rendered":"\n<p>When we write programs in the Python language, we constantly do operations on lists and loops. While doing these operations, python offers us three useful functions that enable us to write our codes in a <strong>simple, readable and short way<\/strong>.<\/p>\n\n\n\n<p>In this post, I will explain how to use <strong><code>map, filter and reduce<\/code><\/strong> functions. map and filter functions are <strong>built-in<\/strong> functions. However, since the <code><strong>reduce<\/strong> <\/code>function is available in the <code><strong>functools module<\/strong><\/code>, we need to import it into our script.<\/p>\n\n\n\n<h2>map()<\/h2>\n\n\n\n<p><code>map(function, iterable)<\/code> : The map function takes function and iterable as arguments.<\/p>\n\n\n\n<p><strong>Examples:<\/strong><\/p>\n\n\n\n<p>We want to get 2 times the values \u200b\u200bon our list as a list. We can do this with both the loop and the map function.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">my_list = [1, 2, 3, 4]\ndef two_times(x):\n    return x * 2\n\n#1. Method\nnew_list = []\nfor i in my_list:\n    new_list.append(two_times ( i ))\n\nprint(new_list)  \n# Output : [1, 4, 6, 8]\n\n#2. Method: with map function\noutput = map(two_times, my_list) \n\nprint(list(output)) \n# Output : [1, 4, 6, 8]<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Important<\/strong>: <em>the map function returns an <strong>object<\/strong>.<\/em><\/pre>\n\n\n\n<p>If we want to see the result in a list, it is necessary to convert the result to a list with the <strong>list()<\/strong> function.<\/p>\n\n\n\n<p><strong>Example: <\/strong>Putting two lists into map function<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">list_1 = [1, 2, 3, 4, 5]\nlist_2 = [2, 4, 6, 8]\n\ndef  multiply(x, y):\n    return x* y\n\nresult = map(multiply, list_1, list_2) \n\nprint(list(result)) \n# Output : [2, 8, 18, 32]<\/pre>\n\n\n\n<p>As you can see, the result list is the size of the smallest list.<\/p>\n\n\n\n<p><strong>Example :<\/strong>  Using the map () function with the lambda function <\/p>\n\n\n\n<p>Generally, the map () function is used with the lambda function.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">my_list= [1, 2, 3, 4]\nresult = map(lambda x: x**2, my_list)\n\nprint( list( result ) ) \n# Output : [1, 4, 9, 16]<\/pre>\n\n\n\n<p>The function list can be given as a argument to the map function.<\/p>\n\n\n\n<h2>filter()<\/h2>\n\n\n\n<p><code>filter(function, iterable)<\/code> : The filter function takes function and iterable as arguments. <code>filter()<\/code>\u00a0forms a new list that contains only elements that satisfy a certain condition.<\/p>\n\n\n\n<p><strong>Example :<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">my_list = [2, 8, 4, 1, 6, 3, 7, 8 ,9]\ngreater_than_5 = list( filter( lambda x : x >5 , my_list))\n\nprint( greater_than_5 ) \n# Output : [8, 6, 7, 8, 9]<\/pre>\n\n\n\n<p>As you can see, the object with values \u200b\u200bgreater than 5 is returned as output, and we converted it into a list with the list () function.<\/p>\n\n\n\n<h2>reduce() <\/h2>\n\n\n\n<p><code>reduce(function, iterable)<\/code> : The reduce function takes function and iterable as arguments. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from functools import reduce\n\nnumbers = [1, 3, 5, 7, 9]\nsum_of_numbers = reduce (lambda x, y : x +y, numbers )\nprint( sum_of_numbers )\n\n# Output : 49<\/pre>\n\n\n\n<p>You can understand how the <code>reduce()<\/code> function works from the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"464\" height=\"367\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/01\/image-1.png\" alt=\"\" class=\"wp-image-767\"\/><\/figure>\n\n\n\n<p>Since the map () and filter () functions are built-in functions of Python, they are a bit faster than <strong>for loop<\/strong>.<\/p>\n\n\n\n<p>Finally, these functions should not use too much with lambda functions as it will reduce code readability.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we write programs in the Python language, we constantly do operations on lists and loops. While doing these operations, python offers us three useful functions that enable us to write our codes in a simple, readable and short way. In this post, I will explain how to use map, filter and reduce functions. map [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Map, Filter and Reduce Functions in Python - blog website<\/title>\n<meta name=\"description\" content=\"When we write programs in the Python language, we constantly do operations on lists and loops. While doing these operations, python offers us three useful functions that enable us to write our codes in a simple, readable and short way. In this post, I will explain how to use map, filter and reduce functions. map and filter functions are built-in functions. However, since the reduce function is available in the functools module, we need to import it into our script.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/04\/map-filter-and-reduce-functions-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Map, Filter and Reduce Functions in Python - blog website\" \/>\n<meta property=\"og:description\" content=\"When we write programs in the Python language, we constantly do operations on lists and loops. While doing these operations, python offers us three useful functions that enable us to write our codes in a simple, readable and short way. In this post, I will explain how to use map, filter and reduce functions. map and filter functions are built-in functions. However, since the reduce function is available in the functools module, we need to import it into our script.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/04\/map-filter-and-reduce-functions-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"blog website\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-04T11:00:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-04T11:38:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/01\/image-1.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.etemkeskin.com\/#website\",\"url\":\"https:\/\/www.etemkeskin.com\/\",\"name\":\"blog website\",\"description\":\"Etem KESK\\u0130N\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.etemkeskin.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"tr\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/04\/map-filter-and-reduce-functions-in-python\/#primaryimage\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/01\/image-1.png\",\"contentUrl\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/01\/image-1.png\",\"width\":464,\"height\":367},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/04\/map-filter-and-reduce-functions-in-python\/#webpage\",\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/04\/map-filter-and-reduce-functions-in-python\/\",\"name\":\"Map, Filter and Reduce Functions in Python - blog website\",\"isPartOf\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/04\/map-filter-and-reduce-functions-in-python\/#primaryimage\"},\"datePublished\":\"2021-01-04T11:00:23+00:00\",\"dateModified\":\"2021-01-04T11:38:35+00:00\",\"author\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\"},\"description\":\"When we write programs in the Python language, we constantly do operations on lists and loops. While doing these operations, python offers us three useful functions that enable us to write our codes in a simple, readable and short way. In this post, I will explain how to use map, filter and reduce functions. map and filter functions are built-in functions. However, since the reduce function is available in the functools module, we need to import it into our script.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/04\/map-filter-and-reduce-functions-in-python\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/04\/map-filter-and-reduce-functions-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/04\/map-filter-and-reduce-functions-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\/\/www.etemkeskin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Map, Filter and Reduce Functions in Python\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\",\"name\":\"etemkeskin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/#personlogo\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"caption\":\"etemkeskin\"},\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/author\/etemkeskinyahoo-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/771"}],"collection":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/comments?post=771"}],"version-history":[{"count":34,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/771\/revisions"}],"predecessor-version":[{"id":810,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/771\/revisions\/810"}],"wp:attachment":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/media?parent=771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/categories?post=771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/tags?post=771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}