Pythonic Magic: Lambda expressions

All Justin Bieber needs is a Beauty and a Beat. Personally, I am happy with a Beauty and Python. Yeah, honestly, python offers one so much, that it’s difficult to ask for more (except for speed maybe, but that’s a tradeoff for python’s amazing expressiveness. And people try to address it as we speak).

So, as you may already have understood, in this second article at my featured series of Pythonic Magic, I am going to talk about Lambda Expressions. But what are Lambda Expressions, one might wonder. Lambda expressions are one of the most powerful (in my opinion) features of python, but people easily tend to misunderstand it or ignore it all together. Which is bad. ‘Cause it is awesome!

Lambda expressions are essentially a language construct that allows one to create anonymous (that is, not bound by any name) functions (more rather function objects) at runtime. That’s it! Really! I will admit that it’s implementation may be hard for some new python programmers to grasp, but once you get the hold of it, it’s really not that difficult. Before we go any further, it would be useful to provide an example of what a Lambda Expression looks like.

Supposing we want to create a function that returns the square of its input. In python, we would do something like this:


>>> def square(x):
...     return x * x
...

Pretty straight forward if you ask me. Nothing magical in this. Now let’s take a look at the output the above function produces:


>>> square(3)
9
>>> square(2.2)
4.840000000000001

Again, nothing magical (if you ignore that number following the trailing zero’s there, it’s all normal. That behavior is also normal, and has to do with hardware limitations on floating point number calculations.) Now that we got this, it’s time to see how the above could be implemented using Lambda Expressions.


>>> g = lambda x: x ** 2 # ** is the exponent operator in python. I could have also said x * x, but I refrained to do so, for clarity's sake
>>> g
<function <lambda> at 0x7f17656045f0>
>>> g(2)
4

Very nice if you ask me. As far as the lambda expression is concerned, it can do pretty much anything that the named function can do. And as you can see in my code snippet, using the lambda keyword, you get to have a lambda function object. Having a function object means that the object is callable (I mean obviously, it’s a function), which in python means that you can bind to any reference, and still call it using the parenthesis as in g(2).

If it still is kind of obscure to you, then maybe it should be worth for me to try to explain it a little bit better. Lambda forms have this general form: lambda [parameter list]: expression This is essentially the keyword lambda, followed by a number of parameters, and an expression, the value of which gets returned when you call the lambda function object.


>>> g = lambda x: x ** 2 # In this case x is the parameter, it is used in the expression to calculate the square of it, and the value of the calculated expression (in this case, the square) gets returned when the object is called.

Now I may like to call it with one parameter, but there is really no one stopping you from calling it with more than one parameters. For example take a look at the following


>>> sum = lambda x, y: x + y
>>> sum(4, 2)
6

>>> # Essentially the same as
... def sum(x, y):
...     return x + y
...

Of course I barely even touched the matter. However I believe that lambdas are a very powerful feature, which although not terribly useful all the time, may be a killer feature for when you really need that. If you feel like you want to know more about lambda expressions, I formed a neat list with various links explaining more than I am on the matter:

Advertisements

Pythonic Magic: List Comprehensions

Disclaimer: This will be the beginning of a series of articles, inside which I will select some of the features in python that I like the most and why I do like them, along with some description and usage of the feature at hand.

Disclaimer: I do not claim to be an expert in python (or any other language for that matter). Anything that follows is my own personal opinion, and may even be wrong, as it is dependent on my understanding on the matter. However I am doing my best to make sure that I do not include wrong information.

—————————————————————————————————————-

You may have heard that the Python programming language is easy to both learn and use. You should slap those that told you so, for blatantly lying to you. Python is not easy to learn. It is *very* easy. *Very very easy*. As a matter of fact it’s so easy that even babies could learn python.  Hyperbole, but you get the point.

I am not going to go to lengths as to why you should learn python. As a matter of fact, a google search can possibly give you many reasons to do so. However the ultimate reason for one to learn python, is to simply want to do so. Why you would want to do so, is an entirely different matter which I will not discuss here, or I will miss the original subject of this article. Which is: wait for it… List Comprehensions! Who could imagine!

Before I go on to explain what list comprehensions are, maybe I should give a description of what lists in python are. Lists are well…lists. Pretty surprising huh? No, on a more serious note, lists in python are a data structure, that acts like a container for any amount of objects, in a given order. If you already have been introduced to programming via another programming language, you may be able to think of the list as an array of objects, which has variable length. In fact Lists are much more powerful than that, following the tradition that Python follows, which is to ensure that you have all the tools that you may possibly need to work right out of the box. In fact, lists (and the other data structures that python provides by default, such dictionaries, sequences, tuples, etc) are so powerful and versatile, that 99% of the time, you will not need to use anything else than what Python already provides. Below is a demonstration of the lists in python.

>>> my_list = [3, 4, 5] # this creates a list with 3 integer objects
>>> my_list
[3, 4, 5]

>>> my_list = [] # this creates an empty list
>>> my_list
[]

>>> my_list.append(5) # this adds 5 to the list
>>> my_list
[5]

>>> my_list.insert(0,3) # this adds 3 in the first position of the list
>>> my_list
[3, 5] # python like many other languages is 0 indexed. This means that list indices begin with 0 instead of 1.

>>> my_list.remove(3) # nothing unexpected here
>>> my_list
[5]

>>> item = my_list.pop() # removes the object from the list, and returns it to be stored at item
>>> item
5

>>> my_list
[]

This pretty much sums the basic usage of Lists in python. I won’t go to any greater extend to because that would doesn’t concern this article. If you would like to read more about lists and their usage, I recommend that you study the official Python tutorials on Lists.

Now that we have talked about Lists, it’s time to talk about List Comprehensions. List Comprehensions are simply a sophisticated way to create Lists that have specific contents. If that doesn’t help you, maybe we should look at an example.

Let’s say we want to create a list of the even numbers in the range between 0 up to (but not including) 10. Normally you would do something like this:

>>> my_list = [] # constructing an empty list
>>> for number in range(10):
        if number % 2 == 0:
            my_list.append(number)
>>> my_list
[0, 2, 4, 6, 8]

List comprehensions allow you to do just the same thing, just times easier (and more sexy, by the way):

>>> even_nums = [x for x in range(10) if x % 2 == 0]
>>> even_nums
[0, 2, 4, 6, 8]

Holly floppy McBaggies. Did you see that? Human words fail to describe the Beauty and the Epic-ness in this.

You might be wondering now. How could I possibly use this to get the most out of my life  it? Well to be honest, there are many ways you could possibly use this feature. Yours trully uses List Comprehensions to create Lists out of  Strings (which are Immutable, that means they are unable to be changed. The reason is here.) to allow for easier manipulation of those. For example:

>>> name = 'fotis'

>>> name[1] = 'u'
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

>>> name_list = [letter for letter in name] # creating a list out of name's characters
>>> name_list
['f', 'o', 't', 'i', 's']
>>> name_list[1] = 'u'

>>> name = ''.join(name_list)
>>> name
'futis'

As you can see we are talking about an extremely powerful feature, that one could use to create lists that have objects that are computed on the fly. As to the possible usage cases for this? Only your fantasy could possibly stop you from using it.