My Meeting with a Lambda Expression
La-La-La-Lambda expressions baby…
When I first skimmed through Python’s documentation, I saw some spooky phrase called lambda expressions. I figured I’d check them out, but once I did, I regretted my decision. They terrified me, so I skedaddled out of that page.
Those hieroglyphics meant nothing, and I knew I’d never cross paths with them…
Until yesterday.
While analyzing my Twitter data with Python, I had to manipulate column values. So I whipped up a short function to apply to the columns. But something felt off. I knew there was a more efficient way to write the code you see below.
I went to my buddy ChatGPT and asked him (yes I’m not calling ChatGPT “it”) what to do. And he told me to use a lambda expression.
When I saw he had the audacity to say that to me, I slammed my head against my keyboard and screamed, “No! No! They’re back!”
But when I came to my senses, I kept my mind open and investigated what these cheeky expressions were. And that’s when I realized their true power.
Just look at the code below…
A lambda expression made my code 3x shorter and easier to understand (for someone who knows Python at least).
When I started writing this blog post, I planned on explaining what the heck lambda expressions are and how to use them in code. I mean, after just a few minutes of researching I fully understood how they work.
But now I’m having second thoughts.
Even though I understand how and when to use them, I still can’t explain them in simple terms. And that was supposed to be the goal of this 66-day experiment.
Hopefully, over the next 55 days, I’m able to write about them again and actually explain what they do. But until then, I’m glad to know how to use it.