List Comprehension in Python

Ethan Jarrell Ethan Jarrell
February 21, 2019 Big Data, Cloud & DevOps

Lists are a basic building block for a developer in any language. Python is no different. However at first, list comprehension can sometimes seem confusing. Since it is not an available feature in all languages, it may also be a concept that is completely unfamiliar. Some popular languages that include list comprehension are JavaScript, Perl 6, C# and of course, Python.

List Comprehension in Python

Today, we’ll take a look at exactly how list comprehension works in Python. In this post, we’ll cover the following concepts:

  1. What is List Comprehension?
  2. What are the advantages of List Comprehension?
  3. What are some ways that List Comprehension can be used?
  4. What are some common examples of List Comprehension?
  5. When should List Comprehension be used, as opposed to other methods?

What is List Comprehension?

At its most basic level, list comprehension is a syntactic construct for creating lists from existing lists. This definition describes some of the uses that we’ll talk about later. If we don’t need to make a new list, but simply print results from an existing list, then list comprehension wouldn’t really be helpful to us. In Python, there are many ways to create lists.

If we wanted to create an empty list, we could declare it as such:

listA = []

Or, if we wanted to create a list with values in it, we could do it this way:

listB = [1,2,3,4]

If we wanted to create a list of 10 items, each item having the same value, we could also do the following:

listB = [1]*10
# which would give us the following output:
 
>>listB
>>1,1,1,1,1,1,1,1,1,1
 
 

Another way to create a list would be looping through an existing list, and appending or inserting data from the first list into a second list.

This might look like the following:

listA = []
listB = [1,2,3,4,5,6]
for
number in listB:
    if number % 2 == 0:
        listA.append(number)
 
 

Here, we are looping over listB, and if the current value in the list is even, we append it to listA. This is a case where we might use list comprehension. Let’s first consider the syntax of a standard loop like the one above, and compare that with the syntax for the same concept, but using list comprehension instead.

Syntax of a for loop:

Which is equivalent to this list comprehension:

[ expression for item in list if conditional ]

An expression is a fancy way of saying “output”. So in each of these cases the output is the result of executing some code. In our previous example, the output was appending something to a new list.

Also in both a traditional for loop and in list comprehension, we have the list to be iterated over, the item or iterable in the list, and a conditional statement.

Let’s take a closer look at the syntax of a for loop:

In a Python for loop, we would start the loop, have a conditional statement if necessary, followed by an execution of a block of code. In the above example, we can see how this would work, along with the expected output.

Here’s how we could rewrite this same piece of code with list comprehension:

Here, we can see that all of the same building blocks that we used in our for loop still exist in the list comprehension, albeit in a slightly different order.

Advantages of List Comprehension

This brings us to the next topic, the advantages of list comprehension. The above example, while slightly less cumbersome than the for loop, can still be consolidated into an even more concise format. One of the big advantages of list comprehension is that they allow developers to write less code that is often easier to understand.

Here’s how we could further simplify the above example:

Here we’ve taken our original three lines of code, and reduced it to an easy to read single line of code.

Other advantages:

List comprehension is usually faster, but only if it is actually being used to create a new list. If you notice, in the first example, we included the append method in the for loop, but when we completely consolidated our code, we left the append method out of the list comprehension completely. This is because in Python, we don’t need to load the append attribute off of the list and call it as a function. Instead, in a comprehension, a specialized LIST_APPEND bytecode is generated for a fast append onto the result list.

In the first example, the append() method would be necessarily called in every iteration of the loop. This means, if our loop had been one million iterations, the append() method would have been called one million times, making it noticeably slower than list comprehension.

How can list comprehension be used?

The above example could be called a filter function. We are taking an existing list, and filtering that list to make a new list. In this case it was to get either the odd or even numbers from a sequence, but the same could be done with other filtering scenarios like generating the fibonacci sequence.

Here are some other examples of ways we might use list comprehension:

  1. To filter an existing list.
  2. To generate a list of data
  3. Flattening a multidimensional list

We’ve already seen an example of how we might use list comprehension to filter the results of one list into a new list. Now let’s look at how we might use it to generate a list of data.

In a previous post, we looked at how we could use python to determine the compound interest on a loan.

Below, we’ll generate a list of dicts, each dict containing a loan with a different principal value, and final value. The final value in each dict will be determined by the initial value we give it, based on the formula for determining compound interest. This formula and dict generation will be stored in it’s own function, but in the loop, and list comprehension, we’ll call the function based on a range of numbers in the loop.

Let’s take a look at the for loop first:

Each time the for loop iterates over a number in the range we’ve set, it then calls the newDict function, and appends the result of that function to our newList. If we print the results, we should see a list of dicts, each containing a unique set of data.

If we rewrite this using list comprehension, we can again avoid using the append method, saving valuable resources on such a large data set.

Here’s what that would look like:

Finally, let’s take a look at how we might use list comprehension to flatten a multidimensional list in Python.

If we were to do this with a for loop, it would likely need to be a nested loop, similar to the following:

We can see here how this might come in handy, if we had a list of lists of names, and needed them quickly flattened into a single list.

Rewriting this code using list comprehension would look like the following:

When should list comprehension be used?

We talked earlier about one of the advantages of list comprehension. This advantage was that we didn’t need to use the append() method to create a new list. List comprehension is great for creating new lists for many reasons:

  1. The code is more concise
  2. The code is generally more readable
  3. The code, in most cases, will run faster

However, this may not always be what is needed. In some cases, we may simply need to print the results of a list as they are iterated over. In which case, list comprehension provides not obvious advantage. In fact, we would end up creating an unneeded list at that point, using unnecessary memory. When deciding whether or not list comprehension would be a better choice than a loop, we can simply ask ourselves, “am I creating a new list?”

If the answer is “yes”, then list comprehension is probably the preferred method in doing so. If not, than a good old fashioned for loop is probably better.

  • Experfy Insights

    Top articles, research, podcasts, webinars and more delivered to you monthly.

  • Ethan Jarrell

    Tags
    Data Science
    Leave a Comment
    Next Post
    Data Science Project Flow for Startups

    Data Science Project Flow for Startups

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    More in Big Data, Cloud & DevOps
    Big Data, Cloud & DevOps
    Cognitive Load Of Being On Call: 6 Tips To Address It

    If you’ve ever been on call, you’ve probably experienced the pain of being woken up at 4 a.m., unactionable alerts, alerts going to the wrong team, and other unfortunate events. But, there’s an aspect of being on call that is less talked about, but even more ubiquitous – the cognitive load. “Cognitive load” has perhaps

    5 MINUTES READ Continue Reading »
    Big Data, Cloud & DevOps
    How To Refine 360 Customer View With Next Generation Data Matching

    Knowing your customer in the digital age Want to know more about your customers? About their demographics, personal choices, and preferable buying journey? Who do you think is the best source for such insights? You’re right. The customer. But, in a fast-paced world, it is almost impossible to extract all relevant information about a customer

    4 MINUTES READ Continue Reading »
    Big Data, Cloud & DevOps
    3 Ways Businesses Can Use Cloud Computing To The Fullest

    Cloud computing is the anytime, anywhere delivery of IT services like compute, storage, networking, and application software over the internet to end-users. The underlying physical resources, as well as processes, are masked to the end-user, who accesses only the files and apps they want. Companies (usually) pay for only the cloud computing services they use,

    7 MINUTES READ Continue Reading »

    About Us

    Incubated in Harvard Innovation Lab, Experfy specializes in pipelining and deploying the world's best AI and engineering talent at breakneck speed, with exceptional focus on quality and compliance. Enterprises and governments also leverage our award-winning SaaS platform to build their own customized future of work solutions such as talent clouds.

    Join Us At

    Contact Us

    1700 West Park Drive, Suite 190
    Westborough, MA 01581

    Email: support@experfy.com

    Toll Free: (844) EXPERFY or
    (844) 397-3739

    © 2023, Experfy Inc. All rights reserved.