Why Your Lambda Function Does Not Work

<span>Photo by <a href=”https://unsplash.com/@luthermeb?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCop
<span>Photo by <a href=”https://unsplash.com/@luthermeb?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCop
Photo by Luther Bottrill on Unsplash

Introduction

Basis usage of lambda

square = lambda x: x**2 
print(square(4))
#Output: 16
cryptocurrencies = [('Bitcoin', 10948.52),('Ethereum', 381.41),('Tether', 1.00), ('XRP', 0.249940),
('Bitcoin Cash', 231.86), ('Polkadot', 4.91),
('Binance Coin', 27.02), ('Chainlink', 10.47),
('Litecoin', 48.20), ('EOS', 2.69),
('TRON', 0.027157), ('Neo', 24.29),
('Stellar', 0.077903), ('Huobi Token', 4.91)]
top5_by_name = sorted(cryptocurrencies,
key=lambda token: token[0].lower())[0:5]
print(top5_by_name)
#Output: [('Binance Coin', 27.02), ('Bitcoin', 10948.52), ('Bitcoin Cash', 231.86), ('Chainlink', 10.47), ('EOS', 2.69)]
lowest = min(cryptocurrencies, key=lambda token: token[1]) print(lowest)
#Output: ('TRON', 0.027157)
highest = max(cryptocurrencies, key=lambda token: token[1]) print(highest)
#Output: ('Bitcoin', 10948.52)
highest_in_local_currency = lambda exchange_rate: highest[1] * exchange_rate
highest_sgd = highest_in_local_currency(1.38)
print(highest_sgd)
#Output: 15108.9576
highest = ('Bitcoin', 10948.52) 
mon_highest = lambda exchange_rate: highest[1] * exchange_rate
highest = ('Bitcoin', 10000)
tue_highest = lambda exchange_rate: highest[1] * exchange_rate
print("Mon:", mon_highest(1.36))
print("Tue:", tue_highest(1.36))
Image for post
Image for post
converters = [lambda exchange_rate: crypto[1] * exchange_rate for crypto in cryptocurrencies] for c in converters: 
print(c(1.36))
Image for post
Image for post

Runtime data binding

mon_highest = lambda exchange_rate,highest = highest: highest[1] * exchange_rate tue_highest = lambda exchange_rate,highest = highest: highest[1] * exchange_rate
converters = [lambda exchange_rate,crypto = crypto: crypto[1] * exchange_rate for crypto in cryptocurrencies]
list(map(lambda crypto: crypto[1] * 1.36, cryptocurrencies))

Written by

Resources and tutorials for python, data science and automation solutions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store