A compendium is “a collection of concise but detailed information about a particular subject, especially in a book or other publication” — Oxford Dictionary.

As the title suggest, this article will talk about defining a function, lambda function, and list comprehensions, all of which are python shortcuts that allow you to write codes in a concise manner.

It is built on top of two other articles I have written, Python Vocabs 101 and Python Sentences 101.

Defining a function

If you want to create your own vocabulary, i.e. function, in Python, you can use the def keyword.

Defining your own function can be useful if there is no other functions or methods that will do the desired outcomes, if you haven’t found such functions or methods, or simply trying to understand how functions work and improve your python skills.

Creating your own function also best if you want to have flexibility in your codes.

In new_function below, it will print out “Hello World” everytime you call the function.

You can have input/s in your function to make it more useful. These inputs are called parameters or arguments.

Scope

In order to proceeed in defining a function, it is important to understand the concept of scope.

Scope is a region or a part of program where a variable or an object belongs, and in effect accessible.

You can have a built-in scope, where the variable that belongs here are pre-defined in the Python itself such as print(), type(), and list().

You can see all the objects that belongs to the builtin scope by calling the dir functions with _builtins_ argument. dir() also belongs to the builtin scope.

Another scope is Local Scope which resides inside a function. An object or variable that is defined inside a function belongs solely inside a function and therefore can only be accessed inside the function.

Below, we are trying to use the local_variable inside b_function and it is giving us an error.

This is because when we call b_function, it tries to look for local_variable and it can’t find it inside the function.

The last scope is Global Scope. It is located in the main part of the whole Python code and variables that are defined here can be accessed anywhere in the script / code.

You can explicitly define a variable or object that belong to a Global Scope inside a function by using the global keyword.

In the example above, we define the a_variable inside e_function with the keyword global.

The function does not print anything when it is called but when we pass the a_variable into the print function outside of the e_function, it is not throwing an error and print the variable.

This is only possible if we at least call the e_function once. Otherwise, the a_variable would not have been defined.

Undefined number of argument with *args and *kwargs

Sometime we want to have flexibilty in the arguments being passed down to a function as in some scenario, said function might recieve one argument and at other times, it might be more than one. We can simply use ***** in the parameter to specify this.

You can also define which argument to print from the parameters passed into the argument like below.

If the parameters contain keywords and you want to be flexible in the number of parameters being passed on, you can use double asterisk ******.

Lambda Function

Lamda function is a nameless function. It is very powerful way of writing a quick function without having to define it explicitly using keyword def. It is a simpler way of writing a function.

The limitation is it can only have one expression but it can take many arguments.

The syntax is “lambda arguments : expression

Alternatively, we can put the argument after writing the lambda function in a bracket as well as wrapping the lambda function in the bracket.

This way of writing is called Immediately Invoked Function Expression or IIFE, prononuce “iffy”.

Lambda is most useful when it is used with other functions.

Let’s demonstrate it with a DataFrame.

Let’s use apply() function to change the content of the DataFrame. Using argument axis equal to 0 will apply the expression to each column. Whereas if it is equal to 1, it will be applied to each row.

In the example above, we apply and False to all the boolean value on each row of “Dollar Currency”. As according to the Truth Table, anything and False will become False.

We can also use filter() to see how many True are there in the Data Frame.

In this example, we use map() function to apply the lambda expression, putting the values into lower case, we defined inside map() to df1.

List Comprehensions

To put it simply, it is a for loop in a one-liner.

With List Comprehensions you can write a much simpler code with the following syntax:

[ output expression for iterator variable in iterable ]

For example, we want to print the number between 0 to 9, this is the one-liner in list comprehensions.

Comparatively, this is the form in the normal for loop.

Additionally, you can have a nested list comprehension by putting the whole syntax inside the output expression section of a list comprehension wrapped with square bracket.

A good example of this is when we want to make a matrix in a one-liner.

Let mix it up by randomise the number generated. The output is a two-dimensional list.

Lets add a condition to the list comprehension.

In this example we want to have a list of even numbers from the matrix list.

In the example above, we essentially have two for loops in a one-liner.

This is essential as we have to make the two-dimensional matrix list into a single dimentional list so we can do the condition.

It is equivalent to the below for loops.

Conclusion

As we practice and learn more about a programming language, having the knowledge and ability to write efficient codes add to our skills repertoire.

The ability to write lambda functions, list comprehensions, and defining a function are a good tools to have when writing python codes. It could summarise your code into the least amount of lines written as possible and this also demonstrate your ability to understand high-level coding.

However, the only downside of these shortcuts are the readability for new coders who are not familiar with these tools.

Thank you for reading!

Product Manager, Infrastructure and Support Guy, and an aspiring Data Scientist