Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Python Example - Lambda Functions

Lambda functions are anonymous inline functions that can be used as arguments to other functions. Lambda functions are useful for sorting lists by a specific key and for finding the maximum or minimum value in a list based on a key. An example shows sorting a list of albums by the artist key using a lambda function and finding the most recent album in a dataset by year using max() and a lambda key.

Uploaded by

lolitaferoz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Python Example - Lambda Functions

Lambda functions are anonymous inline functions that can be used as arguments to other functions. Lambda functions are useful for sorting lists by a specific key and for finding the maximum or minimum value in a list based on a key. An example shows sorting a list of albums by the artist key using a lambda function and finding the most recent album in a dataset by year using max() and a lambda key.

Uploaded by

lolitaferoz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lambda Functions

• A lambda function is a one-line mini-function defined on the fly


• They can be used anywhere a function is required
• For example, here’s a simple function double that doubles an argument x by 2
def double(x):
return x * 2

double(4)
• We can also define a lambda function double_l that does the same thing
double_l = lambda x: x * 2 #given parameter x, return x * 2
double_l(4)

IPNrEoSpSerTtyHoRfOPUeGnnH
Lambda Functions – With Sorting
• Lambda functions are especially useful as arguments to other functions
• For example, in the sorted function:
sorted(list, key) – sorts the list based on the optional key (lambda function)
• key optionally specifies a function that tells sorted what to sort by
• The default value (None) compares the elements directly

IPNrEoSpSerTtyHoRfOPUeGnnHE
Lambda Functions – With Sorting
• What if we want to sort our albums by artist (“Artist”)?
albums_sorted = sorted(albums, key = lambda x: x["Artist"])
print(albums_sorted)
• When sorting albums, use key to compare them
• key is a lambda function -- given parameter x (row), use the “Artist” column to compare

IPNrEoSpSerTtyHoRfOPUeGnnHE
Lambda Functions
• Some other functions that accept lambda functions as arguments:
max(list, key) – returns the maximum value from the list based on the optional key
(lambda function)
• key optionally specifies a function that tells max what to sort by
• The default value (None) compares the elements directly

min(list, key) – returns the minimum value from the list based on the optional key (lambda
function)
• key optionally specifies a function that tells min what to sort by
• The default value (None) compares the elements directly

IPNrEoSpSerTtyHoRfOPUeGnnH
lambda Functions – Exercise
• What’s the Album, Artist, and release Year for the most recent album in the data?
• Hint: Ignore albums that do not have a valid year!
valid_albums = [row for row in albums if is_valid_year(row['Year'])]
album_max = max(valid_albums, key = lambda x: x["Year"])
print(album_max["Album"], album_max["Artist"], album_max["Year"])

IPNrEoSpSerTtyHoRfOPUeGnnH

You might also like