Search

Using Python Lambda, All, And Any Functions To Verify A Food Menu

It was recently reported that the Russian opposition leader, Alexei Navalny, was in coma from suspected food poisoning. That was a disturbing news that made me start thinking of how to create a function that verifies a menu for good and bad foods. I did some thinking and decided that the right functions to use for the verification process were the python all, any and lambda functions.

verifying menu using python lambda, all and any functions
 

But before I start describing the verification process, let us talk a little about these functions.

The Python All Function

The Python all function will return True if all the elements in an iterable are True or if the iterable is empty. The syntax for the python all function is all(iterable). If you want to know what an iterable means, you can check out this post on iterables.

Let us take some examples to demonstrate how it works. We will use a python list for the python all examples.

As you can see when you run the example above, all the elements need to be True or it needs to be an empty list for the python all function to return True.

Note that on a dictionary, the function works on the keys of the dictionary. You can make out examples of your own and try them out.

Also, on numbers, when the number is 0, it is evaluated to be False as a Boolean expression in python and True on all other numbers.

The Python Any Function

The python any function will return True if any element of the iterable is True and will return False if none of the elements is True. For an empty list it will return False. The syntax of the any function is any(iterable).

Here is a python any function example using a python list.

You can try it out on another iterable like a string or dictionary. In dictionaries, the python any function iterates through the keys.

One more python function you need to know about for us to write this code beautifully are python lambda functions.

What are python lambda functions?

Python lambda functions are anonymous python functions which are created with the lambda keyword. They can be used to replace function objects but are restricted in syntax to a single expression. Lambda expressions are just semantic sugar for a normal function definition. They also have scopes like normal functions through which they reference variables.

The syntax of a lambda function or lambda expression is “lambda parameters: expression”. The expression is first followed by the lambda keyword, parameters represent what is passed to the function and the expression is the code that you want to implement in the function. The parameter and expression are separated by a colon.

Given the attributes of python lambda functions above, let me demonstrate how we can create one. For example, if we want to write a single expression that cubes some numbers. Instead of resorting to a normal function, since it is a single expression, we can use a lambda expression inside another function, this time, the python map function. This is an example of a python function inside another function.


cubed = list(map(lambda x: x ** 3,  [1, 2,3,4]))
print(cubed)

You can see from above that the lambda function is used inside the map function and cubes each of the numbers in the list. The lambda function contains just a single expression, x ** 3.

Now that we have all we need to carry out the menu verification program, let’s begin.

The menu verification program

So, we want to write a program that when given a list of recommended foods we want to verify each of the menu for the day whether that menu should be accepted or not. For a menu to be accepted, all the foods have to be in the recommended_foods list and none of the foods should be in the junk_foods list. So, let’s have a go at a menu and see what happens as we write the code.

Elegant, not so? Be creative. Try out your own food test for the recommended and junk foods. Use the all, any, and lambda functions to test out your skills in python.

Happy pythoning.

No comments:

Post a Comment

Your comments here!

Matched content