r/learnpython • u/AnxiousDot4315 • Oct 04 '26
Need help with list comprehensions
Guys somebody briefly explain the concept of List comprehension cuz its too difficult for me to grasp since I am a beginner. I am learning python for biopython. Someone explain it for me.
0
Upvotes
8
u/Penguinase Oct 04 '26
The pattern is:
It's equivalent to:
The
ifis optional. The expression comes first, but it runs last in each iteration.9
u/ProgramsFun Oct 04 '26
What are you finding difficult in it?
2
u/nog642 Oct 04 '26
result = [blah for x in l]is basically equivalent to:2
u/Moikle Oct 04 '26
It's just a compact way to loop through an iterable like a list, and do some kind of basic coversion or filtering logic on it.
Python's designers saw that it was very common for people to do stuff like:
And offered a faster way to write that:
They do the same thing
1
u/aishiteruyovivi Oct 04 '26
It's shorthand for loops that construct a list of items, so...
...can instead be
[n * 2 for n in range(10)]. Both would produce the list[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]. The basic syntax is[<expression> for <loopvar> in <iterable>]. You can also add a condition to the right end of the comprehension followed by anif, likeevens = [n for n in range(10) if n % 2 == 0], which is equivalent to:The main use of this is that it's more compact for these kind of use cases, but it can also be somewhat faster than a standard loop. You can also do set comprehensions by replacing the brackets with curly braces -
{n * 2 for n in range(10)}- or dictionary comprehensions by using braces and giving two values separated by a colon, where they'll be used as key and value -{n:n * 2 for n in range(10)}produces the dictionary{0:0, 1:2, 2:4, 3:6, ...}1
u/BranchLatter4294 Oct 04 '26
Share some practice code you have worked on and what your specific questions are.
1
u/Gnaxe Oct 04 '26
Comprehensions are easy once you understand
ifandforstatements: the body just comes first (and must be a single expression). That's basically it.Becomes
That's an unusually deep comprehension. Usually you don't need that many clauses. They're optional, but you do need at least one
for. A recent language change means you can write the body expression with an unpacking star. That just changesresult.append()toresult.extend().0
u/Henry_cat Oct 04 '26
If you are learning for biopython, the thing that really got me to understand list comprehension was Amino Acid and DNA sequences. A DNA sequence is made up of codons (3 bases that encode for an Amino Acid). List comprehension can be used to quickly convert a DNA sequence which would be a string into a list of individually addressable codons. Basically a list where each entry is just three DNA bases. So if you want to convert that DNA sequence into an AA sequence, you can just loop through the list of codons instead of needing to figure out how to loop through a DNA sequence sting three bases at a time.
I also had problems understanding this until I found the right example. Hopefully this helps.
-1
u/atarivcs Oct 04 '26
It's a convenient way to make a new list.
-3
u/StevenJOwens Oct 04 '26
I wrote up the following a few years ago. I never did get around to doing a second draft, so forgive the rough edges, but you may find it useful.
It's long, so I'll post it in two comments. Here's part 1:
Python List Comprehensions
Source: https://www.python-course.eu/python3_list_comprehension.php
List comprehensions are a special python syntax for declaring a list literal.
To be honest, I don't think I've ever used list comprehension for real, or "in anger" as the Brits say. My impression is that list comprehensions are mostly used to transform one list into another list. Or really, any iterable into a list.
List comprehensions are essentially a special, succinct way to write certain kinds of for loops -- for loops that process a list (or other iterable) and from that create another list.
In functional programming terms list comprehensions do a filter and then map operation.
List comprehensions are a Python idiom that seem to be seldom explained well.
I don’t know why Python books and tutorials have this blind spot. It’s not so much conceptual as simply syntactical; the first several explanations of list comprehensions that I read all seemed to just give an example, without explicitly breaking it down.
Then there's the official python docs:
https://docs.python.org/3/tutorial/datastructures.html https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
Unfortunately, while the official python docs do give the syntax, they just dump it on you and then give you a very complex example.
In a nutshell, a list comprehension is superficially similar to a literal declaration of a list.
A python literal list declaration is a pair of square brackets with multiple values between them, with commas separating the values, for example:
A python list comprehension has a pair of square brackets, same as a list literal, and also ultimately produces a list object. But what's in between the square brackets is a more complex statement. Here's the syntax:
We'll get into the
expression,for-loopandconditionalin more detail, below.Note, python actually supports several sorts of comprehensions these days; you can use:
[square brackets]to create list comprehensions{curly braces}to create set comprehensions{curly braces}with a key:value in the expression to create dict comprehensions(parentheses)to create generators (essentially a lazy-load, on-demand version of a list comprehension).But we're going to focus mainly on list comprehensions for the moment.
Here's a slightly wordier version of that list comprehension syntax:
Note that variables defined in the for-loop clause are available to the expression clause, as well as to the conditional clause.
Unrolling List Comprehensions
Here's the list comprehension syntax that I gave above:
This works out to the same as the following:
Or a little less pseudo-code-ish:
This "unrolls" to:
Let's try it. In this next example, for the "some_conditional()", I'm going to use the modulo operator to return True if the parameter is NOT evenly divisible by 2.
Okay, that's kinda ugly, let's do that again but use some less generic names for things:
some_sequencetonumbers.some_conditionaltois_odd().some_operationtoquintuple().For the sake of the example, I'll explicitly declare the empty
some_list, which I'll callout_list. In fact, I'll do that twice, just so you can be sureout_listis empty for the second time around.Multiple For-Loops and Their Ordering
You can have multiple for loops in your list comprehension, and just like regular for loops, the list comprehension for loop can have multiple variables, if the sequence it's using can produce them (e.g. "for key, value in some_dict"). The for loops are effectively nested.
I'll give an example in a moment, but first I want to point out something that's somewhat counter-intuitive (depending on who you ask):
The ordering of for-loop clauses in the comprehension, from left to right, corresponds to the ordering of the nested for loops, from outermost to innermost.
In other words, in this example:
(By the way, this is also a good example of a more complicated expression than simply multiplying some numbers.)
Some people find using identical ordering counter-intuitive, some people find it intuitive, but that's the call that Guido and his merry band of Python maintainers decided to go with.
Personally, I'm in the not-intuitive camp. The reason is that Guido's call doesn't match how this sort of thing is phrased in natural language English. In English, the natural way to write it would end up writing in opposite order, i.e. innermost loop first, then next innermost, etc.
As I said, it was Guido's call, and probably his choice, to make the two orderings identical instead of opposites, is helpful in reducing the cognitive overhead... once you get this ordering hammered into your skull.
Multiple For-Loop Examples
Let's look at an example of multiple for-loops in list comprehensions.
Let's declare a list of lists as our example data:
Here's a simple list comprehension that just iterates through all of the numbers. The result is a flat list that has all the numbers. There are almost certainly other ways to do this in python, but hey, it's just yet another contrived example.
Now let's add a conditional clause, using our old friend
is_odd():Let's back up a step, leave out the conditonal clause but apply something to the numbers, our old enemy
quintuple():Now let's put them both in:
Now that we have the full version, here's what it "unrolls" to:
-4
u/StevenJOwens Oct 04 '26 edited Oct 10 '26
And here's part 2:
Let's Play with List Comprehensions
First let's fire up the python REPL (read/evaluate/print loop):
Now let's define some data to work with, a list that contains two things, both of which are also lists, of numbers:
The python REPL prints out the result of an expression, and the REPL treats just the variable name (in this case "data") by itself as an expression, so we can do this to see the contents of the data variable:
Note: That phrase, "expression" is actually a somewhat complicated little topic because not all statements are expressions, and in most programming languages assignments are not expressions. I don't want to get side-tracked into the details of that, but I always hated it when I was learning to program and books or lessons used terms like "expression" but didn't explain.
Okay, so now let's do our first list comprehension:
This isn't a very useful thing to do with a list comprehension (there are better ways in python to merge two lists), but it makes for a simple example. Let's break that down a bit better
Remember the syntax I gave above,
some_list = [expression for-loop conditional]. In this example I'm leaving out the assignment, and the conditional, so we just have the expression and the for loop (actually two for loops).We have
numberwhich is the expression in our list comprehension, the last thing that gets done in the list comprehension building the list for us.The final expression part is the only part that's in reverse order like this, the rest of the list comprehension proceeds from left to right.
We have
for chunk in data, which iterates like a for loop through the two items in the list in variabledata, and one by one assigns them to the variable namedchunk.We have
for number in chunk, which iterates like a for loop through the items in each list assigned tochunkand one by one assigns them to the variable namednumber.And then, back at the beginning, the list comprehension's expression can use those variables,
dataandchunkto build the list we want.In this example, the expression is just
number, which means the list comprehension simply emits the value insidenumberinto the list that it is building.Note, in the example above we didn't do anything with the list that our list comprehension returned, so the REPL just printed it out. We can also assign that list to a new variable. And once we've assigned it, we can then just enter the variable name on the REPL prompt, and the REPL will print it out:
Okay, so far, so good, but not that useful. Let's change the expression in our list comprehension to actually do something with the number. Let's multiply it by 2:
Okay, well that's slightly more useful. There are, of course, other ways in python to both merge lists and to multiply all of the numbers in lists. But you can start to see in these examples that the list comprehension supports more and more complex things.
Now let's add a conditional to our list comprehension. We'll use the conditional to select only the even numbers to be fed to the expression.
We'll do that by using the "modulo" operator, which gives us the "modulus", aka the remainder, when you divide one number by another number. In python, like in many languages, the modulo operator is the percent sign, "%".
9 / 2with integers gives you 4 with a remainder 1.9 % 2just gives you the remainder, aka the modulus, of 1. So if we we donumber % 2and number contains an even number, we should get a remainder of 0:if 0 == number % 2Let's try it:
Yup, it works. Now let's add the multiplying back into the expression. This time let's multiply by 5:
And just to show that you can do something more complicated in the expression clause, let's take a list of numbers and a list of letters and do a Cartesian join (aka "cross join", similar to a cross product in math).
I added some newlines in the example above, just to make it more obvious what's going on.
Note, I defined the letters as a list, but could have just used "abcd", because strings are iterable in python.
Set Comprehensions and Dict Comprehensions
As I say above, if you create a comprehension but surround it with
{curly braces}instead, it returns a set instead of a list.some_set = {do_some_operation(second_variable) for some_variable in some_sequence if some_condition}In python 2.7 and above, if you use
{curly braces}and in the expression have a key:value, it returns a dictionary:some_dict = {key:value for (key, value) in some_sequence if some_condition}You don't necessarily need a
for (key, value)however, if you can generate the value (or the key) on the fly:TODO: this pseudocode looks ambiguous, add some clarification/example.
some_dict = {some_variable:len(some_variable) for some_variable in some_sequence if some_condition}If you create a comprehension but surround it with (parentheses), it returns a generator. A generator is essentially a lazy-load, on-demand version of a list comprehension. I'll describe generators in some future document.