Skip to Content
Learn Python by Building Data Science Applications
book

Learn Python by Building Data Science Applications

by Philipp Kats, David Katz
August 2019
Beginner
482 pages
12h 56m
English
Packt Publishing
Content preview from Learn Python by Building Data Science Applications

Understanding anonymous (lambda) functions

In some cases, it is convenient to declare simple one-time functions in place; for that, we can use lambdas – anonymous functions (they are anonymous in the sense that you won't store them in-memory, so there is no name for them). Lambdas use simplified syntax:

lambda <arguments>: <code> 

Note that there is no return keyword – lambda will return the result of the code automatically. It also does not accept multiline code.

Take a look at this example:

models = [    ‘Bimbus-3000',    ‘Nimbus-1000',    ‘Timbus-2000']sort(models, key=lambda x: x[-4:])

The output for the preceding code would be as follows:

>>> [‘Nimbus-1000',‘Timbus-2000', ‘Bimbus-3000']

Here, the array of strings are sorted – however, the string's ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Data Science

Python for Data Science

Yuli Vasiliev
Introduction to Machine Learning with Python

Introduction to Machine Learning with Python

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9781789535365Supplemental Content