avatarJ3

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3359

Abstract

s-built_in">var</span><span class="hljs-number">2</span></pre></div><div id="9aa5"><pre><span class="hljs-function"><span class="hljs-title">t2</span><span class="hljs-params">(<span class="hljs-number">5</span>)</span></span></pre></div><div id="b166"><pre>10</pre></div><p id="c1c4">Now use Map plus Lambda together:</p><p id="dea1"># Using Map function plus Lambda function (what it is build for!):</p><div id="130f"><pre>seq = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>] <span class="hljs-built_in">map</span>(<span class="hljs-built_in">lambda</span> <span class="hljs-built_in">var</span>:<span class="hljs-built_in">var</span><span class="hljs-number">2</span>, seq)</pre></div><div id="cb70"><pre><<span class="hljs-built_in">map</span> <span class="hljs-built_in">at</span> <span class="hljs-number">0x7f0640b37b38</span>></pre></div><p id="f1ec"># Casting Map to a List:</p><div id="3ba0"><pre>list(<span class="hljs-built_in">map</span>(<span class="hljs-built_in">lambda</span> <span class="hljs-built_in">var</span>:<span class="hljs-built_in">var</span><span class="hljs-number">2</span>, seq))</pre></div><div id="1e64"><pre><span class="hljs-string">[2, 4, 6, 8, 10]</span></pre></div><p id="1f44">Complete code:)</p><div id="175e"><pre>seq = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>] <span class="hljs-built_in">print</span>(list(<span class="hljs-built_in">map</span>(<span class="hljs-built_in">lambda</span> <span class="hljs-built_in">var</span>:<span class="hljs-built_in">var</span><span class="hljs-number">2</span>, seq)))</pre></div><div id="34cc"><pre><span class="hljs-string">[2, 4, 6, 8, 10]</span></pre></div><p id="44dc">👉Jupiter notebook <a href="https://github.com/giljr/my_jupyter_notebook">link </a>:)</p><p id="9a29">👉<a href="https://github.com/giljr/my_jupyter_notebook">git</a></p><h2 id="64b9">Credits & References</h2><p id="f0a9"><a href="https://towardsdatascience.com/lambda-functions-with-practical-examples-in-python-45934f3653a8">Lambda Functions with Practical Examples in Python — How, when to use, and when not to use Lambda functions</a> by <a href="https://towardsdatascience.com/lambda-functions-with-practical-examples-in-python-45934f3653a8">towardsdatascience.com</a></p><h1 id="5d03">Related Posts</h1><p id="7e0e"><b>00</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/lambda-in-python-421b0c18e825"><b>Lambda in Python </b></a>— Python Lambda Desmistification (this one)</p><p id="4a17"><b>01</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/send-emails-using-python-jupyter-notebook-94d14a5a5655"><b>Send Email in Python</b></a> — Using Jupyter Notebook — How To Send Gmail In Python</p><p id="578f"><b>02</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/automate-your-email-marketing-with-python-f0d68234b789"><b>Automate Your Email With Python & Outlook</b></a><b> </b>— How To Create An Email Trigger System in Python</p><p id="64c2"><b>03</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/manipulating-files-with-python-3f9a781287e9"><b>Manipulating F

Options

iles With Python</b></a> — Manage Your Lovely Photos With Python!</p><p id="0d72"><b>04</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/pandas-dataframe-advanced-48f83a5b097f"><b>Pandas DataFrame Advanced </b></a>— A Complete Notebook Review</p><p id="a090"><b>05</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/is-this-leap-year-python-calendar-3d1a61f2c4a7"><b>Is This Leap Year? Python Calendar</b> </a>— How To Calculate If The Year Is Leap Year and How Many Days Are In The Month</p><p id="bd1c"><b>06</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/list-comprehension-in-python-c22c4b0a6a8a"><b>List Comprehension In Python </b></a>— Locked-in Secrets About List Comprehension</p><p id="857b"><b>07</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/graphs-in-python-b7d243737b77"><b>Graphs — In Python </b></a>— Extremely Simple Algorithms in Python</p><p id="2523"><b>08</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/decorator-in-python-62c00f7e818"><b>Decorator in Python</b></a> — How To Simplifying Your Code And Boost Your Function</p><p id="feae"><b>10</b>#Episode#PurePythonSeries — <a href="https://readmedium.com/cs50-a-taste-of-python-a4ac87883ff4"><b>CS50</b></a><b> — A Taste of Python</b> — Harvard Mario’s Challenge Solver \o/</p><p id="84af"><b>11</b>#Episode#PurePythonSeries — Python — <a href="https://readmedium.com/python-send-email-using-smtp-6ecf0b1dd608"><b>Send Email Using SMTP</b></a> — Send Mail To Any Internet Machine (SMTP or ESMTP)</p><p id="4114"><b>12#</b>Episode#PurePythonSeries — <a href="https://readmedium.com/advanced-python-technologies-d3dbdf1d70cb"><b>Advanced Python Technologies</b> </a><a href="https://readmedium.com/advanced-python-technologies-d3dbdf1d70cb">qrcode, Speech Recognition in Python, Google Speech Recognition</a></p><p id="4be4"><b>13#</b>Episode#PurePythonSeries — <a href="https://readmedium.com/advanced-python-technologies-ii-33d2d6888583"><b>Advanced Python Technologies II</b></a> — qFace Recognition w/ Jupyter Notebook & Ubuntu</p><p id="f864"><b>14#</b>Episode#PurePythonSeries — <a href="https://readmedium.com/advanced-python-technologies-iii-ac92cd677e5e"><b>Advanced Python Technologies III</b> </a>— Face Recognition w/ Colab</p><p id="1544"><b>15#</b>Episode#PurePythonSeries — <a href="https://readmedium.com/iss-tracking-project-python-af4b5fa47a28"><b>ISS Tracking Projec</b></a><b>t </b>— Get an Email alert when International Space Station (ISS) is above of us in the sky, at night</p><p id="9624"><b>16#</b>Episode#PurePythonSeries — <a href="https://readmedium.com/using-gemini-chat-on-collab-2626fb035176"><b>Using Gemini Chat on Collab</b></a><b> </b>— Random Number Generation, List Manipulation & Rock-Paper-Scissors Game Implementations</p><p id="56f5"><b>17#</b>Episode#PurePythonSeries — Python — <a href="https://readmedium.com/python-basics-2ce557a80f42"><b>Basics</b> </a>— Functions, OOP, file handling, calculator, loops</p><p id="26f9"><b>18#</b>Episode#PurePythonSeries — Python — <a href="https://readmedium.com/efficient-file-handling-in-python-0d952971ebc9"><b>Efficient File Handling in Python</b></a><b> </b>— Best Practices and Common Methods</p><blockquote id="1f13"><p>Edited at Dec_2021 — completing the code to run in pycharm:)</p></blockquote></article></body>

Lambda in Python

Python Lambda Desmistification — #PurePythonSeries — Episode #00

This post attempts to demystify Lambda in Python.
Fig 1. Lambda Expression: lambda function:iterable

Simply put, a lambda function is just like any normal python function, except that it has no name when defining it, and it is contained in one line of code. A lambda function evaluates an expression for a given argument. You give the function a value (argument) and then provide the operation (expression) — From TowardsDatascience.com.

LAMBDA

How To Get A Lambda Expression just from times2 function?

Here is the original function:

def times2(var):
  return var*2

The times2 function can be written in one line:

Rewriting it in one line:

def times2(var):return var*2

Now, get rid of the def keyword:

times2(var):return var*2

Now, to get a lambda expression just get rid of the name of the function (typing lambda in its place — because lambda is an anonymous function);

lambda(var):return var*2

And get rid of the parentheses adding space in front (cleaning the code:);

lambda var:return var*2

And finally, get rid of return word (its function is assumed by the colon ), like this :)

lambda var:var*2

It Reads: lambda takes var and returns var*2.

Just in one line. Simple Like That! Awesome!

This is not usual for lambda, but it is a feasible use (saving lambda times2 to var t2):

t2 = lambda var:var*2
t2(5)
10

Now use Map plus Lambda together:

# Using Map function plus Lambda function (what it is build for!):

seq = [1,2,3,4,5]
map(lambda var:var*2, seq)
<map at 0x7f0640b37b38>

# Casting Map to a List:

list(map(lambda var:var*2, seq))
[2, 4, 6, 8, 10]

Complete code:)

seq = [1,2,3,4,5]
print(list(map(lambda var:var*2, seq)))
[2, 4, 6, 8, 10]

👉Jupiter notebook link :)

👉git

Credits & References

Lambda Functions with Practical Examples in Python — How, when to use, and when not to use Lambda functions by towardsdatascience.com

Related Posts

00#Episode#PurePythonSeries — Lambda in Python — Python Lambda Desmistification (this one)

01#Episode#PurePythonSeries — Send Email in Python — Using Jupyter Notebook — How To Send Gmail In Python

02#Episode#PurePythonSeries — Automate Your Email With Python & Outlook — How To Create An Email Trigger System in Python

03#Episode#PurePythonSeries — Manipulating Files With Python — Manage Your Lovely Photos With Python!

04#Episode#PurePythonSeries — Pandas DataFrame Advanced — A Complete Notebook Review

05#Episode#PurePythonSeries — Is This Leap Year? Python Calendar — How To Calculate If The Year Is Leap Year and How Many Days Are In The Month

06#Episode#PurePythonSeries — List Comprehension In Python — Locked-in Secrets About List Comprehension

07#Episode#PurePythonSeries — Graphs — In Python — Extremely Simple Algorithms in Python

08#Episode#PurePythonSeries — Decorator in Python — How To Simplifying Your Code And Boost Your Function

10#Episode#PurePythonSeries — CS50 — A Taste of Python — Harvard Mario’s Challenge Solver \o/

11#Episode#PurePythonSeries — Python — Send Email Using SMTP — Send Mail To Any Internet Machine (SMTP or ESMTP)

12#Episode#PurePythonSeries — Advanced Python Technologies qrcode, Speech Recognition in Python, Google Speech Recognition

13#Episode#PurePythonSeries — Advanced Python Technologies II — qFace Recognition w/ Jupyter Notebook & Ubuntu

14#Episode#PurePythonSeries — Advanced Python Technologies III — Face Recognition w/ Colab

15#Episode#PurePythonSeries — ISS Tracking Project — Get an Email alert when International Space Station (ISS) is above of us in the sky, at night

16#Episode#PurePythonSeries — Using Gemini Chat on Collab — Random Number Generation, List Manipulation & Rock-Paper-Scissors Game Implementations

17#Episode#PurePythonSeries — Python — Basics — Functions, OOP, file handling, calculator, loops

18#Episode#PurePythonSeries — Python — Efficient File Handling in Python — Best Practices and Common Methods

Edited at Dec_2021 — completing the code to run in pycharm:)

Python3
Lambda Expressions
How To
Pure Python
Python Functions
Recommended from ReadMedium