{"id":78,"date":"2025-10-21T04:35:12","date_gmt":"2025-10-21T04:35:12","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=78"},"modified":"2025-12-17T07:48:39","modified_gmt":"2025-12-17T07:48:39","slug":"lesson-17-lambda-map-filter-reduce-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=78","title":{"rendered":"Lesson 17: Lambda, Map, Filter, Reduce in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p>To learn <strong>anonymous functions<\/strong> with <code>lambda<\/code>, and functional programming tools like <code>map<\/code>, <code>filter<\/code>, and <code>reduce<\/code> for concise and efficient data processing.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. Lambda Functions<\/strong><\/h2>\n\n\n\n<p>A <strong>lambda function<\/strong> is a small <strong>anonymous function<\/strong> defined with the <code>lambda<\/code> keyword.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lambda arguments: expression\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Example 1: Square of a Number<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>square = lambda x: x**2\nprint(square(5))\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>25\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Example 2: Sum of Two Numbers<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add = lambda a, b: a + b\nprint(add(10, 15))\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>25\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>2. Map Function<\/strong><\/h2>\n\n\n\n<p><code>map()<\/code> applies a function to <strong>each element of an iterable<\/strong>.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>map(function, iterable)\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Example: Square a List of Numbers<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nums = &#91;1, 2, 3, 4, 5]\nsquares = list(map(lambda x: x**2, nums))\nprint(squares)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 4, 9, 16, 25]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf0 <strong>3. Filter Function<\/strong><\/h2>\n\n\n\n<p><code>filter()<\/code> returns elements of an iterable that <strong>satisfy a condition<\/strong>.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>filter(function, iterable)\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Example: Even Numbers from a List<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nums = &#91;1, 2, 3, 4, 5, 6]\nevens = list(filter(lambda x: x % 2 == 0, nums))\nprint(evens)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;2, 4, 6]\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 <strong>4. Reduce Function<\/strong><\/h2>\n\n\n\n<p><code>reduce()<\/code> applies a function <strong>cumulatively to the items<\/strong> of an iterable to reduce it to a single value.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Requires <code>functools<\/code> module.<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>from functools import reduce\n\nnums = &#91;1, 2, 3, 4, 5]\nsum_all = reduce(lambda x, y: x + y, nums)\nprint(sum_all)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>15\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Example 2: Product of Numbers<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>product = reduce(lambda x, y: x * y, nums)\nprint(product)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>120\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>5. Combining Map, Filter, Reduce<\/strong><\/h2>\n\n\n\n<p>\u2705 <strong>Example: Sum of Squares of Even Numbers<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from functools import reduce\n\nnums = &#91;1, 2, 3, 4, 5, 6]\nevens = filter(lambda x: x % 2 == 0, nums)\nsquares = map(lambda x: x**2, evens)\nsum_squares = reduce(lambda x, y: x + y, squares)\nprint(sum_squares)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>56\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf0d <strong>6. Real-Life Example \u2013 Processing Grades<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>grades = &#91;80, 90, 75, 88, 95]\n\n# Increase each grade by 5\ngrades_inc = list(map(lambda x: x + 5, grades))\n\n# Keep only grades &gt;= 90\ntop_grades = list(filter(lambda x: x &gt;= 90, grades_inc))\n\n# Sum of top grades\ntotal_top = reduce(lambda x, y: x + y, top_grades)\n\nprint(\"Grades +5:\", grades_inc)\nprint(\"Top Grades:\", top_grades)\nprint(\"Sum of Top Grades:\", total_top)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Grades +5: &#91;85, 95, 80, 93, 100]\nTop Grades: &#91;95, 93, 100]\nSum of Top Grades: 288\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To learn anonymous functions with lambda, and functional programming tools like map, filter, and reduce for concise and efficient data processing. \ud83e\udde9 1. Lambda Functions A lambda function is a small anonymous function defined with the lambda keyword. Syntax: \u2705 Example 1: Square of a Number Output: \u2705 Example 2: Sum of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions\/79"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}