This piece is a really short and quick one. We will see which is the best, why and when to use either a python list comprehension or a python for loop when dealing with your lists.
For this piece, we will base our conclusion on whether to use python list comprehension or the other option based on a simple metric system of our own. We will award points based on these criteria:
Clean code is often more desirable. So, when you want to do any list manipulation, it is best to have this in mind, since dealing with lists may lead you down a path of unequivocally entangled state.
So, we will use a simple example of where we want to get even numbers in a list. This is our list:
sample_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Now, using a for loop, this is what we would have:
sample_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = []
We will need to create a new list to hold our list of even numbers, since it is not possible to update the current list whilst iterating through it.
sample_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = []
for number in sample_numbers:
if number % 2 == 0:
even_numbers.append(number)
Then we would proceed with appending the even numbers. So that is good and seems neat and concise, BUT wait till you see what we get with a list comprehension.
Using a python list comprehension:
sample_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sample_numbers = [number for number in sample_numbers if number % 2 == 0]
It is just a one liner! This is cleaner and so concise, because code is written for the machine, not the programmer, the programmer gets the documentation!
So, it is a unanimous vote that python list comprehension takes the point here!
Well, with flexibility, we will be considering how potent either the python list comprehension or python for loop is, when it comes to meeting the programmer’s needs.
For example, you want to write the results of a list to a file (don’t worry, we will have a simple example for this). Well, if that is the case, then it is best to go with the python for loop, since it allows for a clear logical breakdown of tasks in a procedural manner.
So, we will have a simple list of logs:
logs = ['log_1', 'log_2', 'log_3', 'log_4', 'log_5']
We declare our list of logs.
logs = ['log_1', 'log_2', 'log_3', 'log_4', 'log_5']
for log in logs:
# your logic for writing the log to it's file
pass
Then we simply iterate them and then we can create their logs. That is really simple and flexible. So, how well does python list comprehension compare with the same?
Well, not so good. With a python list comprehension, you are locked in with what you have in the list,with no much room for growth or flexibility. Well, you may go a head and be tempted try something like this:
logs = ['log_1', 'log_2', 'log_3', 'log_4', 'log_5']
[do_log(log) for log in logs]
def do_log(log):
print (log)
The catch with the above code is that, for each item, you will have to make the function call, and spoiler alert! Function calls in python are slow! Now if you had a million items in your list – well, you can imagine.
So, python for loop will take this point by a landslide!
Well, the performance of an algorithm is determined mostly by how efficient it is. An efficient algorithm, among other things, has to be fast and scalable.
So, assuming that there are no side effects (as you have seen in the flexibility section) and that the core is simply calculation, how will this turn out? Well, let’s find out.
Let’s assume that we have 1 million subscribers and we want to find out of those subscribers, how many read this article more than twice:
subscribers = [
{
# other attributes here
'view_count': 1
},
{
# other attributes here
'view_count': 4
},
{
# other attributes here
'view_count': 2
},
{
# other attributes here
'view_count': 0
},
# more subscribers to the millionth subscriber
]
# for the for loop, we have a list of subsequent subscribers, we call them the loyal_ones
loyal_ones = []
for subscriber in subscribers:
if subscriber.get('view_count') >= 2:
loyal_ones.append(subscriber)
As you see, we need another list to hold our loyal subscribers (are you a loyal subscriber) so that we can display them. Well, this however has a problem. We will need a physical address to store this list, which may grow exponentially, based on how many loyal subscribers (like you) we have.
Even if the loyal_ones list will be available only in the life cycle of the function or script it appears in, it is really inefficient when scalability is of a concern. If we have 500k loyal_ones, then we have an idle list of 500k loyal_ones just sitting around.
Now, to make things more complicated, let’s assume that this method is called by 100k viewers, to view the list of loyal_ones. You clearly see how this will soon be a problem.
Well, thankfully, python list comprehension makes things cheaper and scalable. We simply do this:
subscribers = [
{
# other attributes here
'view_count': 1
},
{
# other attributes here
'view_count': 4
},
{
# other attributes here
'view_count': 2
},
{
# other attributes here
'view_count': 0
},
# more subscribers to the millionth subscriber
]
subscribers = [loyal_one for loyal_one in subscribers if loyal_one.get('view_count') >= 2]
As you can see, the results are the same, but way more efficient, since we only mutate the existing address location items, and not create a new one.
So, clearly, python list comprehension wins this one as it is fast and scalable over the python for loop, in such a scenario.
Well, as a programmer, I have this saying: Show me your code and I will tell you the kind of programmer you are. It may be out of line, but it is correct (Sam Wilson – The Red Falcon)
A list comprehension is more professional as compared to a for loop, when the same operation is of concern. As you saw in the Cleanliness section, you get the same from a python list comprehension as you would with a python for loop, for fewer lines of code.
So, clearly, a list comprehension is more professional and so it takes the points for this one.
Finally, there is the question of portability. Portability here is with respect of how easy it is to transform a logic from Python to other programming languages.
Let’s assume that you want to change your code from Python to JavaScript. How easy will it be to do so if you had a for loop in place or if you had a list comprehension in place.
Well, long story short, for loop is a global standard in almost all programming languages when it comes to iterating lists / arrays. However, list comprehension, as far as I know, is only present in python.
Therefore, it would be hard to transform a complex python list comprehension into another programming language, than it would to transform a python for loop. So, python for loop takes the point on this one.
Well, time for the verdict:
So, the winner, by 75% wins of the metric measure is (drum rolls) python list comprehension. Your opinion is always welcome!
Well, that is it from this one.