Converting SQL to Lambda Expressions
Now that we have retrieved the data from the database, we can convert the SQL query to lambda expressions in Python. The equivalent lambda expression for the SQL query would be
list(filter(lambda p: p['ListPrice'] > 3000, products))
The list(filter()) function takes a lambda function as its first argument and filters the products list according to the lambda expression. The lambda function takes a single argument, p, which represents each product in the list, and returns True if the ListPrice field of the product is greater than 3000.
Finally, we print the results of the SQL query and the lambda expression to the console. The for loop iterates over each product in the products list and prints it to the console.
print('SQL query:') for product in products: print(product) print('Lambda expression:') for product in list(filter(lambda p: p['ListPrice'] > 3000, products)): print(product)
Output of the SQL Query:
Output of the Lambda Expression:
{'ProductID': 749, 'Name': 'Road-150 Red, 62', 'ProductNumber': 'BK-R93R-62 ', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Red', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 2171.2942, 'ListPrice': 3578.27 , 'Size': '62', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '15', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'} {'ProductID': 750, 'Name': 'Road-150 Red, 44 ', 'ProductNumber': 'BK-R93R-44', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Red', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 2171.2942, 'ListPrice': 3578.27 , 'Size': '44', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '13.77 ', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'} {'ProductID': 751, 'Name': 'Road-150 Red, 48 ', 'ProductNumber': 'BK-R93R-48, 38', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Red', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 2171.2942, 'ListPrice': 3578.27 , 'Size': '48', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '14.13 ', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'} {'ProductID': 752, 'Name': 'Road-150 Red, 52 ', 'ProductNumber': 'BK-R93R-52, 42', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Red', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 2171.2942, 'ListPrice': 3578.27 , 'Size': '52', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '14.42 ', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'} {'ProductID': 753, 'Name': 'Road-150 Red, 56 ', 'ProductNumber': 'BK-R93R-56, 38', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Red', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 2171.2942, 'ListPrice': 3578.27 , 'Size': '56', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '14.68 ', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'} {'ProductID': 778, 'Name': 'Mountain-100 Silver, 38', 'ProductNumber': 'BK-M82S-38', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Silver ', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 1912.1544, 'ListPrice': 3399.99 , 'Size': '38', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '20.35 ', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'} {'ProductID': 771, 'Name': 'Mountain-100 Silver, 42', 'ProductNumber': 'BK-M82S-42', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Silver', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 1912.1544, 'ListPrice': 3399.99, 'Size': '42', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '20.77 ', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'} {'ProductID': 772, 'Name': 'Mountain-100 Silver, 44', 'ProductNumber': 'BK-M82S-44', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Silver', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 1912.1544, 'ListPrice': 3399.99, 'Size': '44', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '21.13 ', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'} {'ProductID': 773, 'Name': 'Mountain-100 Silver, 48', 'ProductNumber': 'BK-M82S-48', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Silver', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 1912.1544, 'ListPrice': 3399.99, 'Size': '48', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '21.42 ', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'} {'ProductID': 774, 'Name': 'Mountain-100 Silver, 38', 'ProductNumber': 'BK-M82S-38', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Black ', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 1898.0944, 'ListPrice': 3374.99 , 'Size': '38', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '20.35 ', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'} {'ProductID': 775, 'Name': 'Mountain-100 Silver, 42', 'ProductNumber' : 'BK-M82S-42', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Black', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 1898.0944, 'ListPrice': 3374.99, 'Size': '42', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '20.77 ', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'} {'ProductID': 776, 'Name': 'Mountain-100 Silver, 44', 'ProductNumber': 'BK-M82S-44', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Black', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 1898.0944, 'ListPrice': 3374.99, 'Size': '44', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '21.13 ', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'} {'ProductID': 777, 'Name': 'Mountain-100 Silver, 48', 'ProductNumber': 'BK-M82S-48', 'MakeFlag': 1, 'FinishedGoodsFlag ': 1, 'Color ': 'Black', 'SafetyStockLevel ': 100, 'ReorderPoint ' : 75, 'StandardCost': 1898.0944, 'ListPrice': 3374.99, 'Size': '48', 'WeightUnitMeasureCode ' : 'LB', 'Weight': '21.42 ', 'ProductCategoryID': 'U', 'ProductModelID': 'HF-0578'}
How to Convert SQL in Lambda Expressions?
Lambda expressions make it simple to define anonymous functions in computer languages. Functions can be used as arguments or returned as values in functional programming paradigms, where they are treated as first-class citizens.
Lambda expressions let you declare a plain function “inline”—without giving it a name—when it has to be written and will only be called once. They are also a great way to streamline your code and make it clearer and more expressive.
In many programming languages, lambda expressions are distinguished by the “lambda” keyword, a list of parameters that are separated by commas, a colon, and the expression that will be evaluated.
Contact Us