Intermediate Python Knowledge

Lambda Functions Aren’t Always Your Best Choices

Don’t reinvent the wheels

Yong Cui
Yong Cui
May 28, 2020 · 4 min read
Photo by Aziz Acharki on Unsplash
lambda arguments: expression

Operations Over Sequences

>>> # Create a list of tuples for sorting
>>> scores = [(95, 98, 99), (94, 97, 93), (95, 92, 93), (100, 95, 98)]
>>> # Sort the list with a lambda function
>>> # The most tedious
>>> sorted(scores, key=lambda x: x[0] + x[1] + x[2])
[(95, 92, 93), (94, 97, 93), (95, 98, 99), (100, 95, 98)]
>>>
>>> # Slightly better
>>> sorted(scores, key=lambda x: sum(x))
[(95, 92, 93), (94, 97, 93), (95, 98, 99), (100, 95, 98)]
>>> # Best, using sum() directly
>>> sorted(scores, key=sum)
[(95, 92, 93), (94, 97, 93), (95, 98, 99), (100, 95, 98)]
>>> # Find out the team with the member having the highest score
>>> # Using lambda
>>> max(scores, key=lambda x: max(x))
(100, 95, 98)
>>>
>>> # Using built-in directly
>>> max(scores, key=max)
(100, 95, 98)

Higher-Order Functions

>>> # Create a list of numbers
>>> numbers = [1, 2, 3, 4, 5]
>>>
>>> # Use the lambda with map()
>>> squares0 = list(map(lambda x: x*x, numbers))
>>> squares0
[1, 4, 9, 16, 25]
>>> # Use the list comphrension
>>> squares1 = [x*x for x in numbers]
>>> squares1
[1, 4, 9, 16, 25]
>>> # List comprehension with conditional
>>> odds_squares = [x*x for x in numbers if x%2]
>>> odds_squares
[1, 9, 25]

Conclusions

The Startup

Get smarter at building your thing. Join The Startup’s +786K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Yong Cui

Written by

Yong Cui

Work at the nexus of biomedicine, data science & mobile dev. Love to write on these technological topics. Follow me @ycui01 on Twitter to get latest articles.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +786K followers.

Yong Cui

Written by

Yong Cui

Work at the nexus of biomedicine, data science & mobile dev. Love to write on these technological topics. Follow me @ycui01 on Twitter to get latest articles.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +786K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium