Python Enhancement Proposals

  • Python »
  • PEP Index »

PEP 572 – Assignment Expressions

The importance of real code, exceptional cases, scope of the target, relative precedence of :=, change to evaluation order, differences between assignment expressions and assignment statements, specification changes during implementation, _pydecimal.py, datetime.py, sysconfig.py, simplifying list comprehensions, capturing condition values, changing the scope rules for comprehensions, alternative spellings, special-casing conditional statements, special-casing comprehensions, lowering operator precedence, allowing commas to the right, always requiring parentheses, why not just turn existing assignment into an expression, with assignment expressions, why bother with assignment statements, why not use a sublocal scope and prevent namespace pollution, style guide recommendations, acknowledgements, a numeric example, appendix b: rough code translations for comprehensions, appendix c: no changes to scope semantics.

This is a proposal for creating a way to assign to variables within an expression using the notation NAME := expr .

As part of this change, there is also an update to dictionary comprehension evaluation order to ensure key expressions are executed before value expressions (allowing the key to be bound to a name and then re-used as part of calculating the corresponding value).

During discussion of this PEP, the operator became informally known as “the walrus operator”. The construct’s formal name is “Assignment Expressions” (as per the PEP title), but they may also be referred to as “Named Expressions” (e.g. the CPython reference implementation uses that name internally).

Naming the result of an expression is an important part of programming, allowing a descriptive name to be used in place of a longer expression, and permitting reuse. Currently, this feature is available only in statement form, making it unavailable in list comprehensions and other expression contexts.

Additionally, naming sub-parts of a large expression can assist an interactive debugger, providing useful display hooks and partial results. Without a way to capture sub-expressions inline, this would require refactoring of the original code; with assignment expressions, this merely requires the insertion of a few name := markers. Removing the need to refactor reduces the likelihood that the code be inadvertently changed as part of debugging (a common cause of Heisenbugs), and is easier to dictate to another programmer.

During the development of this PEP many people (supporters and critics both) have had a tendency to focus on toy examples on the one hand, and on overly complex examples on the other.

The danger of toy examples is twofold: they are often too abstract to make anyone go “ooh, that’s compelling”, and they are easily refuted with “I would never write it that way anyway”.

The danger of overly complex examples is that they provide a convenient strawman for critics of the proposal to shoot down (“that’s obfuscated”).

Yet there is some use for both extremely simple and extremely complex examples: they are helpful to clarify the intended semantics. Therefore, there will be some of each below.

However, in order to be compelling , examples should be rooted in real code, i.e. code that was written without any thought of this PEP, as part of a useful application, however large or small. Tim Peters has been extremely helpful by going over his own personal code repository and picking examples of code he had written that (in his view) would have been clearer if rewritten with (sparing) use of assignment expressions. His conclusion: the current proposal would have allowed a modest but clear improvement in quite a few bits of code.

Another use of real code is to observe indirectly how much value programmers place on compactness. Guido van Rossum searched through a Dropbox code base and discovered some evidence that programmers value writing fewer lines over shorter lines.

Case in point: Guido found several examples where a programmer repeated a subexpression, slowing down the program, in order to save one line of code, e.g. instead of writing:

they would write:

Another example illustrates that programmers sometimes do more work to save an extra level of indentation:

This code tries to match pattern2 even if pattern1 has a match (in which case the match on pattern2 is never used). The more efficient rewrite would have been:

Syntax and semantics

In most contexts where arbitrary Python expressions can be used, a named expression can appear. This is of the form NAME := expr where expr is any valid Python expression other than an unparenthesized tuple, and NAME is an identifier.

The value of such a named expression is the same as the incorporated expression, with the additional side-effect that the target is assigned that value:

There are a few places where assignment expressions are not allowed, in order to avoid ambiguities or user confusion:

This rule is included to simplify the choice for the user between an assignment statement and an assignment expression – there is no syntactic position where both are valid.

Again, this rule is included to avoid two visually similar ways of saying the same thing.

This rule is included to disallow excessively confusing code, and because parsing keyword arguments is complex enough already.

This rule is included to discourage side effects in a position whose exact semantics are already confusing to many users (cf. the common style recommendation against mutable default values), and also to echo the similar prohibition in calls (the previous bullet).

The reasoning here is similar to the two previous cases; this ungrouped assortment of symbols and operators composed of : and = is hard to read correctly.

This allows lambda to always bind less tightly than := ; having a name binding at the top level inside a lambda function is unlikely to be of value, as there is no way to make use of it. In cases where the name will be used more than once, the expression is likely to need parenthesizing anyway, so this prohibition will rarely affect code.

This shows that what looks like an assignment operator in an f-string is not always an assignment operator. The f-string parser uses : to indicate formatting options. To preserve backwards compatibility, assignment operator usage inside of f-strings must be parenthesized. As noted above, this usage of the assignment operator is not recommended.

An assignment expression does not introduce a new scope. In most cases the scope in which the target will be bound is self-explanatory: it is the current scope. If this scope contains a nonlocal or global declaration for the target, the assignment expression honors that. A lambda (being an explicit, if anonymous, function definition) counts as a scope for this purpose.

There is one special case: an assignment expression occurring in a list, set or dict comprehension or in a generator expression (below collectively referred to as “comprehensions”) binds the target in the containing scope, honoring a nonlocal or global declaration for the target in that scope, if one exists. For the purpose of this rule the containing scope of a nested comprehension is the scope that contains the outermost comprehension. A lambda counts as a containing scope.

The motivation for this special case is twofold. First, it allows us to conveniently capture a “witness” for an any() expression, or a counterexample for all() , for example:

Second, it allows a compact way of updating mutable state from a comprehension, for example:

However, an assignment expression target name cannot be the same as a for -target name appearing in any comprehension containing the assignment expression. The latter names are local to the comprehension in which they appear, so it would be contradictory for a contained use of the same name to refer to the scope containing the outermost comprehension instead.

For example, [i := i+1 for i in range(5)] is invalid: the for i part establishes that i is local to the comprehension, but the i := part insists that i is not local to the comprehension. The same reason makes these examples invalid too:

While it’s technically possible to assign consistent semantics to these cases, it’s difficult to determine whether those semantics actually make sense in the absence of real use cases. Accordingly, the reference implementation [1] will ensure that such cases raise SyntaxError , rather than executing with implementation defined behaviour.

This restriction applies even if the assignment expression is never executed:

For the comprehension body (the part before the first “for” keyword) and the filter expression (the part after “if” and before any nested “for”), this restriction applies solely to target names that are also used as iteration variables in the comprehension. Lambda expressions appearing in these positions introduce a new explicit function scope, and hence may use assignment expressions with no additional restrictions.

Due to design constraints in the reference implementation (the symbol table analyser cannot easily detect when names are re-used between the leftmost comprehension iterable expression and the rest of the comprehension), named expressions are disallowed entirely as part of comprehension iterable expressions (the part after each “in”, and before any subsequent “if” or “for” keyword):

A further exception applies when an assignment expression occurs in a comprehension whose containing scope is a class scope. If the rules above were to result in the target being assigned in that class’s scope, the assignment expression is expressly invalid. This case also raises SyntaxError :

(The reason for the latter exception is the implicit function scope created for comprehensions – there is currently no runtime mechanism for a function to refer to a variable in the containing class scope, and we do not want to add such a mechanism. If this issue ever gets resolved this special case may be removed from the specification of assignment expressions. Note that the problem already exists for using a variable defined in the class scope from a comprehension.)

See Appendix B for some examples of how the rules for targets in comprehensions translate to equivalent code.

The := operator groups more tightly than a comma in all syntactic positions where it is legal, but less tightly than all other operators, including or , and , not , and conditional expressions ( A if C else B ). As follows from section “Exceptional cases” above, it is never allowed at the same level as = . In case a different grouping is desired, parentheses should be used.

The := operator may be used directly in a positional function call argument; however it is invalid directly in a keyword argument.

Some examples to clarify what’s technically valid or invalid:

Most of the “valid” examples above are not recommended, since human readers of Python source code who are quickly glancing at some code may miss the distinction. But simple cases are not objectionable:

This PEP recommends always putting spaces around := , similar to PEP 8 ’s recommendation for = when used for assignment, whereas the latter disallows spaces around = used for keyword arguments.)

In order to have precisely defined semantics, the proposal requires evaluation order to be well-defined. This is technically not a new requirement, as function calls may already have side effects. Python already has a rule that subexpressions are generally evaluated from left to right. However, assignment expressions make these side effects more visible, and we propose a single change to the current evaluation order:

  • In a dict comprehension {X: Y for ...} , Y is currently evaluated before X . We propose to change this so that X is evaluated before Y . (In a dict display like {X: Y} this is already the case, and also in dict((X, Y) for ...) which should clearly be equivalent to the dict comprehension.)

Most importantly, since := is an expression, it can be used in contexts where statements are illegal, including lambda functions and comprehensions.

Conversely, assignment expressions don’t support the advanced features found in assignment statements:

  • Multiple targets are not directly supported: x = y = z = 0 # Equivalent: (z := (y := (x := 0)))
  • Single assignment targets other than a single NAME are not supported: # No equivalent a [ i ] = x self . rest = []
  • Priority around commas is different: x = 1 , 2 # Sets x to (1, 2) ( x := 1 , 2 ) # Sets x to 1
  • Iterable packing and unpacking (both regular or extended forms) are not supported: # Equivalent needs extra parentheses loc = x , y # Use (loc := (x, y)) info = name , phone , * rest # Use (info := (name, phone, *rest)) # No equivalent px , py , pz = position name , phone , email , * other_info = contact
  • Inline type annotations are not supported: # Closest equivalent is "p: Optional[int]" as a separate declaration p : Optional [ int ] = None
  • Augmented assignment is not supported: total += tax # Equivalent: (total := total + tax)

The following changes have been made based on implementation experience and additional review after the PEP was first accepted and before Python 3.8 was released:

  • for consistency with other similar exceptions, and to avoid locking in an exception name that is not necessarily going to improve clarity for end users, the originally proposed TargetScopeError subclass of SyntaxError was dropped in favour of just raising SyntaxError directly. [3]
  • due to a limitation in CPython’s symbol table analysis process, the reference implementation raises SyntaxError for all uses of named expressions inside comprehension iterable expressions, rather than only raising them when the named expression target conflicts with one of the iteration variables in the comprehension. This could be revisited given sufficiently compelling examples, but the extra complexity needed to implement the more selective restriction doesn’t seem worthwhile for purely hypothetical use cases.

Examples from the Python standard library

env_base is only used on these lines, putting its assignment on the if moves it as the “header” of the block.

  • Current: env_base = os . environ . get ( "PYTHONUSERBASE" , None ) if env_base : return env_base
  • Improved: if env_base := os . environ . get ( "PYTHONUSERBASE" , None ): return env_base

Avoid nested if and remove one indentation level.

  • Current: if self . _is_special : ans = self . _check_nans ( context = context ) if ans : return ans
  • Improved: if self . _is_special and ( ans := self . _check_nans ( context = context )): return ans

Code looks more regular and avoid multiple nested if. (See Appendix A for the origin of this example.)

  • Current: reductor = dispatch_table . get ( cls ) if reductor : rv = reductor ( x ) else : reductor = getattr ( x , "__reduce_ex__" , None ) if reductor : rv = reductor ( 4 ) else : reductor = getattr ( x , "__reduce__" , None ) if reductor : rv = reductor () else : raise Error ( "un(deep)copyable object of type %s " % cls )
  • Improved: if reductor := dispatch_table . get ( cls ): rv = reductor ( x ) elif reductor := getattr ( x , "__reduce_ex__" , None ): rv = reductor ( 4 ) elif reductor := getattr ( x , "__reduce__" , None ): rv = reductor () else : raise Error ( "un(deep)copyable object of type %s " % cls )

tz is only used for s += tz , moving its assignment inside the if helps to show its scope.

  • Current: s = _format_time ( self . _hour , self . _minute , self . _second , self . _microsecond , timespec ) tz = self . _tzstr () if tz : s += tz return s
  • Improved: s = _format_time ( self . _hour , self . _minute , self . _second , self . _microsecond , timespec ) if tz := self . _tzstr (): s += tz return s

Calling fp.readline() in the while condition and calling .match() on the if lines make the code more compact without making it harder to understand.

  • Current: while True : line = fp . readline () if not line : break m = define_rx . match ( line ) if m : n , v = m . group ( 1 , 2 ) try : v = int ( v ) except ValueError : pass vars [ n ] = v else : m = undef_rx . match ( line ) if m : vars [ m . group ( 1 )] = 0
  • Improved: while line := fp . readline (): if m := define_rx . match ( line ): n , v = m . group ( 1 , 2 ) try : v = int ( v ) except ValueError : pass vars [ n ] = v elif m := undef_rx . match ( line ): vars [ m . group ( 1 )] = 0

A list comprehension can map and filter efficiently by capturing the condition:

Similarly, a subexpression can be reused within the main expression, by giving it a name on first use:

Note that in both cases the variable y is bound in the containing scope (i.e. at the same level as results or stuff ).

Assignment expressions can be used to good effect in the header of an if or while statement:

Particularly with the while loop, this can remove the need to have an infinite loop, an assignment, and a condition. It also creates a smooth parallel between a loop which simply uses a function call as its condition, and one which uses that as its condition but also uses the actual value.

An example from the low-level UNIX world:

Rejected alternative proposals

Proposals broadly similar to this one have come up frequently on python-ideas. Below are a number of alternative syntaxes, some of them specific to comprehensions, which have been rejected in favour of the one given above.

A previous version of this PEP proposed subtle changes to the scope rules for comprehensions, to make them more usable in class scope and to unify the scope of the “outermost iterable” and the rest of the comprehension. However, this part of the proposal would have caused backwards incompatibilities, and has been withdrawn so the PEP can focus on assignment expressions.

Broadly the same semantics as the current proposal, but spelled differently.

Since EXPR as NAME already has meaning in import , except and with statements (with different semantics), this would create unnecessary confusion or require special-casing (e.g. to forbid assignment within the headers of these statements).

(Note that with EXPR as VAR does not simply assign the value of EXPR to VAR – it calls EXPR.__enter__() and assigns the result of that to VAR .)

Additional reasons to prefer := over this spelling include:

  • In if f(x) as y the assignment target doesn’t jump out at you – it just reads like if f x blah blah and it is too similar visually to if f(x) and y .
  • import foo as bar
  • except Exc as var
  • with ctxmgr() as var

To the contrary, the assignment expression does not belong to the if or while that starts the line, and we intentionally allow assignment expressions in other contexts as well.

  • NAME = EXPR
  • if NAME := EXPR

reinforces the visual recognition of assignment expressions.

This syntax is inspired by languages such as R and Haskell, and some programmable calculators. (Note that a left-facing arrow y <- f(x) is not possible in Python, as it would be interpreted as less-than and unary minus.) This syntax has a slight advantage over ‘as’ in that it does not conflict with with , except and import , but otherwise is equivalent. But it is entirely unrelated to Python’s other use of -> (function return type annotations), and compared to := (which dates back to Algol-58) it has a much weaker tradition.

This has the advantage that leaked usage can be readily detected, removing some forms of syntactic ambiguity. However, this would be the only place in Python where a variable’s scope is encoded into its name, making refactoring harder.

Execution order is inverted (the indented body is performed first, followed by the “header”). This requires a new keyword, unless an existing keyword is repurposed (most likely with: ). See PEP 3150 for prior discussion on this subject (with the proposed keyword being given: ).

This syntax has fewer conflicts than as does (conflicting only with the raise Exc from Exc notation), but is otherwise comparable to it. Instead of paralleling with expr as target: (which can be useful but can also be confusing), this has no parallels, but is evocative.

One of the most popular use-cases is if and while statements. Instead of a more general solution, this proposal enhances the syntax of these two statements to add a means of capturing the compared value:

This works beautifully if and ONLY if the desired condition is based on the truthiness of the captured value. It is thus effective for specific use-cases (regex matches, socket reads that return '' when done), and completely useless in more complicated cases (e.g. where the condition is f(x) < 0 and you want to capture the value of f(x) ). It also has no benefit to list comprehensions.

Advantages: No syntactic ambiguities. Disadvantages: Answers only a fraction of possible use-cases, even in if / while statements.

Another common use-case is comprehensions (list/set/dict, and genexps). As above, proposals have been made for comprehension-specific solutions.

This brings the subexpression to a location in between the ‘for’ loop and the expression. It introduces an additional language keyword, which creates conflicts. Of the three, where reads the most cleanly, but also has the greatest potential for conflict (e.g. SQLAlchemy and numpy have where methods, as does tkinter.dnd.Icon in the standard library).

As above, but reusing the with keyword. Doesn’t read too badly, and needs no additional language keyword. Is restricted to comprehensions, though, and cannot as easily be transformed into “longhand” for-loop syntax. Has the C problem that an equals sign in an expression can now create a name binding, rather than performing a comparison. Would raise the question of why “with NAME = EXPR:” cannot be used as a statement on its own.

As per option 2, but using as rather than an equals sign. Aligns syntactically with other uses of as for name binding, but a simple transformation to for-loop longhand would create drastically different semantics; the meaning of with inside a comprehension would be completely different from the meaning as a stand-alone statement, while retaining identical syntax.

Regardless of the spelling chosen, this introduces a stark difference between comprehensions and the equivalent unrolled long-hand form of the loop. It is no longer possible to unwrap the loop into statement form without reworking any name bindings. The only keyword that can be repurposed to this task is with , thus giving it sneakily different semantics in a comprehension than in a statement; alternatively, a new keyword is needed, with all the costs therein.

There are two logical precedences for the := operator. Either it should bind as loosely as possible, as does statement-assignment; or it should bind more tightly than comparison operators. Placing its precedence between the comparison and arithmetic operators (to be precise: just lower than bitwise OR) allows most uses inside while and if conditions to be spelled without parentheses, as it is most likely that you wish to capture the value of something, then perform a comparison on it:

Once find() returns -1, the loop terminates. If := binds as loosely as = does, this would capture the result of the comparison (generally either True or False ), which is less useful.

While this behaviour would be convenient in many situations, it is also harder to explain than “the := operator behaves just like the assignment statement”, and as such, the precedence for := has been made as close as possible to that of = (with the exception that it binds tighter than comma).

Some critics have claimed that the assignment expressions should allow unparenthesized tuples on the right, so that these two would be equivalent:

(With the current version of the proposal, the latter would be equivalent to ((point := x), y) .)

However, adopting this stance would logically lead to the conclusion that when used in a function call, assignment expressions also bind less tight than comma, so we’d have the following confusing equivalence:

The less confusing option is to make := bind more tightly than comma.

It’s been proposed to just always require parentheses around an assignment expression. This would resolve many ambiguities, and indeed parentheses will frequently be needed to extract the desired subexpression. But in the following cases the extra parentheses feel redundant:

Frequently Raised Objections

C and its derivatives define the = operator as an expression, rather than a statement as is Python’s way. This allows assignments in more contexts, including contexts where comparisons are more common. The syntactic similarity between if (x == y) and if (x = y) belies their drastically different semantics. Thus this proposal uses := to clarify the distinction.

The two forms have different flexibilities. The := operator can be used inside a larger expression; the = statement can be augmented to += and its friends, can be chained, and can assign to attributes and subscripts.

Previous revisions of this proposal involved sublocal scope (restricted to a single statement), preventing name leakage and namespace pollution. While a definite advantage in a number of situations, this increases complexity in many others, and the costs are not justified by the benefits. In the interests of language simplicity, the name bindings created here are exactly equivalent to any other name bindings, including that usage at class or module scope will create externally-visible names. This is no different from for loops or other constructs, and can be solved the same way: del the name once it is no longer needed, or prefix it with an underscore.

(The author wishes to thank Guido van Rossum and Christoph Groth for their suggestions to move the proposal in this direction. [2] )

As expression assignments can sometimes be used equivalently to statement assignments, the question of which should be preferred will arise. For the benefit of style guides such as PEP 8 , two recommendations are suggested.

  • If either assignment statements or assignment expressions can be used, prefer statements; they are a clear declaration of intent.
  • If using assignment expressions would lead to ambiguity about execution order, restructure it to use statements instead.

The authors wish to thank Alyssa Coghlan and Steven D’Aprano for their considerable contributions to this proposal, and members of the core-mentorship mailing list for assistance with implementation.

Appendix A: Tim Peters’s findings

Here’s a brief essay Tim Peters wrote on the topic.

I dislike “busy” lines of code, and also dislike putting conceptually unrelated logic on a single line. So, for example, instead of:

instead. So I suspected I’d find few places I’d want to use assignment expressions. I didn’t even consider them for lines already stretching halfway across the screen. In other cases, “unrelated” ruled:

is a vast improvement over the briefer:

The original two statements are doing entirely different conceptual things, and slamming them together is conceptually insane.

In other cases, combining related logic made it harder to understand, such as rewriting:

as the briefer:

The while test there is too subtle, crucially relying on strict left-to-right evaluation in a non-short-circuiting or method-chaining context. My brain isn’t wired that way.

But cases like that were rare. Name binding is very frequent, and “sparse is better than dense” does not mean “almost empty is better than sparse”. For example, I have many functions that return None or 0 to communicate “I have nothing useful to return in this case, but since that’s expected often I’m not going to annoy you with an exception”. This is essentially the same as regular expression search functions returning None when there is no match. So there was lots of code of the form:

I find that clearer, and certainly a bit less typing and pattern-matching reading, as:

It’s also nice to trade away a small amount of horizontal whitespace to get another _line_ of surrounding code on screen. I didn’t give much weight to this at first, but it was so very frequent it added up, and I soon enough became annoyed that I couldn’t actually run the briefer code. That surprised me!

There are other cases where assignment expressions really shine. Rather than pick another from my code, Kirill Balunov gave a lovely example from the standard library’s copy() function in copy.py :

The ever-increasing indentation is semantically misleading: the logic is conceptually flat, “the first test that succeeds wins”:

Using easy assignment expressions allows the visual structure of the code to emphasize the conceptual flatness of the logic; ever-increasing indentation obscured it.

A smaller example from my code delighted me, both allowing to put inherently related logic in a single line, and allowing to remove an annoying “artificial” indentation level:

That if is about as long as I want my lines to get, but remains easy to follow.

So, in all, in most lines binding a name, I wouldn’t use assignment expressions, but because that construct is so very frequent, that leaves many places I would. In most of the latter, I found a small win that adds up due to how often it occurs, and in the rest I found a moderate to major win. I’d certainly use it more often than ternary if , but significantly less often than augmented assignment.

I have another example that quite impressed me at the time.

Where all variables are positive integers, and a is at least as large as the n’th root of x, this algorithm returns the floor of the n’th root of x (and roughly doubling the number of accurate bits per iteration):

It’s not obvious why that works, but is no more obvious in the “loop and a half” form. It’s hard to prove correctness without building on the right insight (the “arithmetic mean - geometric mean inequality”), and knowing some non-trivial things about how nested floor functions behave. That is, the challenges are in the math, not really in the coding.

If you do know all that, then the assignment-expression form is easily read as “while the current guess is too large, get a smaller guess”, where the “too large?” test and the new guess share an expensive sub-expression.

To my eyes, the original form is harder to understand:

This appendix attempts to clarify (though not specify) the rules when a target occurs in a comprehension or in a generator expression. For a number of illustrative examples we show the original code, containing a comprehension, and the translation, where the comprehension has been replaced by an equivalent generator function plus some scaffolding.

Since [x for ...] is equivalent to list(x for ...) these examples all use list comprehensions without loss of generality. And since these examples are meant to clarify edge cases of the rules, they aren’t trying to look like real code.

Note: comprehensions are already implemented via synthesizing nested generator functions like those in this appendix. The new part is adding appropriate declarations to establish the intended scope of assignment expression targets (the same scope they resolve to as if the assignment were performed in the block containing the outermost comprehension). For type inference purposes, these illustrative expansions do not imply that assignment expression targets are always Optional (but they do indicate the target binding scope).

Let’s start with a reminder of what code is generated for a generator expression without assignment expression.

  • Original code (EXPR usually references VAR): def f (): a = [ EXPR for VAR in ITERABLE ]
  • Translation (let’s not worry about name conflicts): def f (): def genexpr ( iterator ): for VAR in iterator : yield EXPR a = list ( genexpr ( iter ( ITERABLE )))

Let’s add a simple assignment expression.

  • Original code: def f (): a = [ TARGET := EXPR for VAR in ITERABLE ]
  • Translation: def f (): if False : TARGET = None # Dead code to ensure TARGET is a local variable def genexpr ( iterator ): nonlocal TARGET for VAR in iterator : TARGET = EXPR yield TARGET a = list ( genexpr ( iter ( ITERABLE )))

Let’s add a global TARGET declaration in f() .

  • Original code: def f (): global TARGET a = [ TARGET := EXPR for VAR in ITERABLE ]
  • Translation: def f (): global TARGET def genexpr ( iterator ): global TARGET for VAR in iterator : TARGET = EXPR yield TARGET a = list ( genexpr ( iter ( ITERABLE )))

Or instead let’s add a nonlocal TARGET declaration in f() .

  • Original code: def g (): TARGET = ... def f (): nonlocal TARGET a = [ TARGET := EXPR for VAR in ITERABLE ]
  • Translation: def g (): TARGET = ... def f (): nonlocal TARGET def genexpr ( iterator ): nonlocal TARGET for VAR in iterator : TARGET = EXPR yield TARGET a = list ( genexpr ( iter ( ITERABLE )))

Finally, let’s nest two comprehensions.

  • Original code: def f (): a = [[ TARGET := i for i in range ( 3 )] for j in range ( 2 )] # I.e., a = [[0, 1, 2], [0, 1, 2]] print ( TARGET ) # prints 2
  • Translation: def f (): if False : TARGET = None def outer_genexpr ( outer_iterator ): nonlocal TARGET def inner_generator ( inner_iterator ): nonlocal TARGET for i in inner_iterator : TARGET = i yield i for j in outer_iterator : yield list ( inner_generator ( range ( 3 ))) a = list ( outer_genexpr ( range ( 2 ))) print ( TARGET )

Because it has been a point of confusion, note that nothing about Python’s scoping semantics is changed. Function-local scopes continue to be resolved at compile time, and to have indefinite temporal extent at run time (“full closures”). Example:

This document has been placed in the public domain.

Source: https://github.com/python/peps/blob/main/peps/pep-0572.rst

Last modified: 2023-10-11 12:05:51 GMT

Python Land

Python List Comprehension: Tutorial With Examples

There’s a concept called set-builder notation in mathematics, also called set comprehension. Inspired by this principle, Python offers list comprehensions. In fact, Python list comprehensions are one of the defining features of the language. It allows us to create concise, readable code that outperforms the uglier alternatives like for loops or using map() .

We’ll first look at the most well-known type: list comprehensions. Once we’ve got a good grasp of how they work, you’ll also learn about set comprehensions and dictionary comprehensions.

Table of Contents

  • 1 What are list comprehensions?
  • 2 Examples of list comprehensions
  • 3 More advanced examples
  • 4 Other comprehensions

What are list comprehensions?

A Python list comprehension is a language construct. It’s used to create a Python list based on an existing list. Sounds a little vague, but after a few examples, that ‘ah-ha!’ moment will follow, trust me.

The basic syntax of a list comprehension is:

The ‘if’-part is optional, as you’ll discover soon. However, we do need a list to start from. Or, more specifically, anything that can be iterated . We’ll use Python’s range() function, which is a special type of iterator called a generator: it generates a new number on each iteration.

Examples of list comprehensions

Enough theory, let’s look at the most basic example, and I encourage you to fire up a Python REPL to try this yourself:

Some observations:

  • The expression part is just x
  • Instead of a list, we use the range() function. We can use [1, 2, 3, 4] too, but using range() is more efficient and requires less typing for longer ranges.

The result is a list of elements, obtained from range() . Not very useful, but we did create our first Python list comprehension. We could just as well use:

So let’s throw in that if-statement to make it more useful:

The if -part acts like a filter. If the condition after the if resolves to True, the item is included. If it resolves to False, it’s omitted. This way, we can get only the even numbers using a list comprehension.

So far, our expression (the x ) has been really simple. Just to make this absolutely clear though, expression can be anything that is valid Python code and is considered an expression. Example:

This expression adds four to x, which is still quite simple. But we could also have done something more complicated, like calling a function with x as the argument:

More advanced examples

You mastered the basics; congrats! Let’s continue with some more advanced examples.

Nested list comprehension

If expression can be any valid Python expression, it can also be another list comprehension. This can be useful when you want to create a matrix:

Or, if you want to flatten the previous matrix:

The same, but with some more whitespace to make this clearer:

The first part loops over the matrix m . The second part loops over the elements in each vector.

Alternatives to list comprehensions

The Python language could do without comprehensions; it would just not look as beautiful. Using functional programming functions like map() and reduce() can do everything a list comprehension can.

Another alternative is using for-loops . If you’re coming from a C-style language, like Java, you’ll be tempted to use for loops. Although it’s not the end of the world, you should know that list comprehensions are more performant and considered a better coding style.

Other comprehensions

If there are list comprehensions, why not create dictionary comprehension as well? Or what about set comprehensions? As you might expect, both exist.

Set comprehensions

The syntax for a Python set comprehension is not much different. We just use curly braces instead of square brackets:

For example:

Dictionary comprehensions

A dictionary requires a key and a value. Otherwise, it’s the same trick again:

The only difference is that we define both the key and value in the expression part.

Get certified with our courses

Learn Python properly through small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

The Python Course for Beginners

Related articles

  • JSON in Python: How To Read, Write, and Parse
  • Python List: How To Create, Sort, Append, Remove, And More
  • Python Set: The Why And How With Example Code
  • Python YAML: How to Load, Read, and Write YAML

Leave a Comment Cancel reply

You must be logged in to post a comment.

Learn Python practically and Get Certified .

Popular Tutorials

Popular examples, reference materials, learn python interactively, python introduction.

  • How to Get Started With Python?
  • Python Comments
  • Python Variables, Constants and Literals
  • Python Type Conversion
  • Python Basic Input and Output
  • Python Operators
  • Precedence and Associativity of Operators in Python (programiz.com)

Python Flow Control

  • Python if...else Statement
  • Python for Loop
  • Python while Loop
  • Python break and continue
  • Python pass Statement

Python Data types

  • Python Numbers, Type Conversion and Mathematics

Python List

  • Python Tuple
  • Python Sets
  • Python Dictionary
  • Python Functions
  • Python Function Arguments
  • Python Variable Scope
  • Python Global Keyword
  • Python Recursion
  • Python Modules
  • Python Package
  • Python Main function (programiz.com)

Python Files

  • Python Directory and Files Management
  • Python CSV: Read and Write CSV files (programiz.com)
  • Reading CSV files in Python (programiz.com)
  • Writing CSV files in Python (programiz.com)
  • Python Exception Handling
  • Python Exceptions
  • Python Custom Exceptions

Python Object & Class

  • Python Objects and Classes
  • Python Inheritance
  • Python Multiple Inheritance
  • Polymorphism in Python(with Examples) (programiz.com)
  • Python Operator Overloading

Python Advanced Topics

  • List comprehension

Python Lambda/Anonymous Function

  • Python Iterators
  • Python Generators
  • Python Namespace and Scope
  • Python Closures
  • Python Decorators
  • Python @property decorator
  • Python RegEx

Python Date and Time

  • Python datetime
  • Python strftime()
  • Python strptime()
  • How to get current date and time in Python?
  • Python Get Current Time
  • Python timestamp to datetime and vice-versa
  • Python time Module
  • Python sleep()

Additional Topic

  • Python Keywords and Identifiers
  • Python Asserts
  • Python Json
  • Python *args and **kwargs (With Examples) (programiz.com)

Python Tutorials

Python Dictionary Comprehension

Python Dictionary fromkeys()

  • Python range() Function
  • Python List insert()

Python List Comprehension

List comprehension offers a concise way to create a new list based on the values of an existing list.

Suppose we have a list of numbers and we desire to create a new list containing the double value of each element in the list.

Python List Comprehension

  • Syntax of List Comprehension

for every item in list , execute the expression if the condition is True .

Note: The if statement in list comprehension is optional.

  • for Loop vs. List Comprehension

List comprehension makes the code cleaner and more concise than for loop.

Let's write a program to print the square of each list element using both for loop and list comprehension.

List Comprehension

It's much easier to understand list comprehension once you know Python for loop() .

  • Conditionals in List Comprehension

List comprehensions can utilize conditional statements like if…else to filter existing lists.

Let's see an example of an if statement with list comprehension.

Here, list comprehension checks if the number from range(1, 10) is even or odd. If even, it appends the number in the list.

Note : The range() function generates a sequence of numbers. To learn more, visit Python range() .

Let's use if...else with list comprehension to find even and odd numbers.

Here, if an item in the numbers list is divisible by 2 , it appends Even to the list even_odd_list . Else, it appends Odd .

Let's use nested if with list comprehension to find even numbers that are divisible by 5 .

Here, list comprehension checks two conditions:

  • if y is divisible by 2 or not.
  • if yes, is y divisible by 5 or not.

If y satisfies both conditions, the number appends to num_list .

  • Example: List Comprehension with String

We can also use list comprehension with iterables other than lists.

Here, we used list comprehension to find vowels in the string 'Python' .

More on Python List Comprehension

We can also use nested loops in list comprehension. Let's write code to compute a multiplication table.

Here is how the nested list comprehension works:

Nested Loop in List Comprehension

Let's see the equivalent code using nested for loop.

Equivalent Nested for Loop

Here, the nested for loop generates the same output as the nested list comprehension. We can see that the code with list comprehension is much cleaner and concise.

Along with list comprehensions, we also use lambda functions to work with lists.

While list comprehension is commonly used for filtering a list based on some conditions, lambda functions are commonly used with functions like map() and filter() .

They are used for complex operations or when an anonymous function is required.

Let's look at an example.

We can achieve the same result using list comprehension by:

If we compare the two codes, list comprehension is straightforward and simpler to read and understand.

So unless we need to perform complex operations, we can stick to list comprehension.

Visit Python Lambda/ Function to learn more about the use of lambda functions in Python.

Table of Contents

  • Introduction

Video: Python List, Set & Dictionary Comprehension

Sorry about that.

Related Tutorials

Python Tutorial

Python Library

Python Simplified

PythonSimplifiedcomLogo

Comprehensive Guide to Python List Comprehensions

  • June 28, 2021
  • Author - Chetan Ambi
  • Category - Python

Python List Comprehensions

Table of Contents

Introduction

Python list comprehensions provide a concise way of creating lists. For example, if you want to write code that squares all the numbers from 1 to 10, you would write as below (the traditional way). First, you need to create an empty list, then loop through all the elements, square each element and then append to the list. 

You can achieve the same result using Python list comprehensions as shown below. As you can see, list comprehensions are readable, concise, and more Pythonic.

Understanding list comprehensions don’t stop here. But there is much to learn about list comprehensions. In this comprehensive guide, we’ll dig deeper and understand more about the list comprehensions. Once you go through this guide, you would have mastered list comprehensions in Python. I know you are excited!! Let’s get started.

Lists are one of the sequence types in Python. Refer to our in-depth article  here   for introduction to sequence types in Python.

List Comprehensions

As mentioned earlier, list comprehensions are concise ways of creating lists. The general syntax of list comprehension looks as below. It consists of three parts: iteration, transformation, and filtering .

list-comprehension-syntax

  • Iteration: This is the first part that gets executed in a list comprehension that iterates through all the elements in the iterable. 
  • Transformation : As the name says transformation will be applied to each element in the iterable. In the above example, we are squaring each element from the iterable.
  • Filter (optional) : This is optional and it filters out some of the elements based on the conditions. The above example loops through all the elements from 1 to 10, filters out odd numbers and considers only positive numbers before applying the transformation.

Let’s see couple more examples of list comprehensions. The below example converts all the strings in the list to upper case.

Here is another example that displays all the numbers less than 50 that are even and divisible by 7.

What happens under the hood

Now that you have understood the syntax and how to write list comprehensions, let’s try to understand what goes on under the hood.

Whenever Python encounters a list comprehension, it creates a temporary function that gets executed during the runtime. The result is stored at some memory location and then gets assigned the variable on the LHS. Refer to the diagram below.

list-comprehension-as-a-function

You can confirm this by running the list comprehension through the dis module. In the below example, the built-in compile() function generates the bytecode of the list comprehension. Then when you run it through the dis module, it generates byte code instructions as below.

Refer to the output of dis below. The bytecode instruction has MAKE_FUNCTION , CALL_FUNCTION . This confirms that Python converts list comprehensions to temporary functions internally.

Since list comprehensions are internally treated as functions, they exhibit some of the properties of functions. List comprehensions also have local and global scopes . In the below example, num is a local variable and factor is a global variable. But you can still access the global variable factor inside the list comprehension.

Nested list comprehensions

It’s possible to write nested list comprehensions meaning you can include a list comprehension within another. One of the uses of nested list comprehensions is that they can be used to create n-dimensional matrices. 

In the example below, we are able to create a 5×5 matrix using nested list comprehensions.

Nested loops in list comprehensions

It’s also possible to include multiple loops in list comprehensions. You can use as many loops as you want. But, as a best practice, you should limit the depth to two so as not to lose the readability. The below example uses two for loops. 

One of the uses of nested loops is to vectorize the matrix (flattening the matrix). In the below example, my_matrix is a 2×2 matrix. Using nested loops, we are able to create a vectorized version.

Multi-line list comprehensions

Most of the examples you have seen are one-line list comprehensions. But note that you can also write multi-line list comprehensions and are preferred when you can’t fit all the instructions in one line. They just increase the readability and won’t offer any added benefit. See an example below.

The same multi-line list comprehension is written in the single line as below. As you can see this is not readable.

List comprehensions with conditionals (if-else)

You can also use if-else conditions with list comprehensions. An example of using the if condition was covered in the syntax section. However, let me provide two examples here one with if and another with if-else .

Using if conditional:

The below example uses the if statement to filter out the languages that start with the letter “P”. Notice that if statement comes after the loop. In general, if condition is used to filtering.

Using if-else conditional:

When the if-else is used with list comprehension, the syntax is a little different compared to comprehension with only if statement. The below example calculates the square even numbers and cube of odd numbers between 1 to 10 using an if-else statement.

Do you want to master Python for else and while else? Please read our detailed article here .

List comprehensions with walrus operator

You can create list comprehensions using the walrus operator := and is also called as “assignment expression”. The walrus operator was first introduced in Python 3.8. As mentioned in PEP 572, it was introduced to speedup the coding by writing short code.

The below example creates a list of even numbers between 0 to 10 using the walrus operator.

List comprehensions Vs map and filter

The map and filter function can also be used as alternatives to list comprehensions as both the functions help us in creating the list object. 

map vs. list comprehension

The below example shows that the list comprehension function can be an alternative to the map function. 

The below code also confirms that list comprehensions are faster than the map function.

filter vs. list comprehension

From the below example, it is evident that list comprehensions can be an alternative to the filter function. 

Even in this case, list comprehensions are faster than filter functions. Refer to the below example.

Set Comprehensions

You can also create set comprehensions but the only difference is they are enclosed within curly brackets { } . The below example creates set comprehension from a list. It converts all the strings in the given list to upper case and creates a set (of course, by removing the duplicates)

Dictionary Comprehensions

Dictionary comprehensions are also enclosed with { } brackets but its contents follow key: value format as you expect. The below example creates a dictionary comprehension from a list. It creates a dictionary with string as key and length of the string as value. 

Generator Expressions

The generator expressions look very similar to list comprehension with the difference is that generator expressions are enclosed within parenthesis ( ) . You could say generator comprehension a lazy version of list comprehension . 

In the below example, we created a list of squares of numbers between 1 to 10 that are even-numbered using generator expression. Note that the output of generator expression is a generator object. To get the result, you need to pass the generator object to the list constructor (or to a tuple or set the constructor based on your need). 

Generator Expressions Vs. List Comprehensions

a) Storage efficiency: A list comprehension stores the entire list in the memory whereas a generator yields one item at a time on-demand. Hence, generator expressions are more memory efficient than lists. As seen from the below example, the same code generator expression takes far less memory.

b) Speed efficiency: List comprehensions are faster than generator expressions. The same code takes 7 seconds for list comprehensions but 9 seconds with generator expressions.

c) Use list comprehensions if you need to iterate over the list multiple times. If not, consider using generator expressions. Because you won’t be able to iterate generator expression again once you already iterated over. See the example below. When you iterate over a second time, it returns an empty list. 

So, which one should you consider? list comprehensions or generator expressions. There is always a trade-off between memory and speed efficiency so you need to choose based on the requirements. 

Do we have tuple comprehensions?

No, there are no tuple comprehensions! As we have gone through earlier, list comprehensions loop through each item in the iterable and append to the list under the hood. You are mutating the list by appending elements to it. List, set and dictionary comprehensions exist because they are mutable. But tuple is an immutable object. Hence, we don’t have tuple comprehensions. 

Refer to this detailed discussion on the same subject on Stack Overflow.

  • List comprehensions provide a concise way of creating lists.
  • Apart from list comprehensions , Python also has set comprehensions , dictionary comprehensions , and generator expressions .
  • Under the hood, list comprehensions converted to equivalent functions.
  • List comprehensions come in different formats  — nested list comprehensions, with nested loop, with multi-lines, with walrus operator, with if and if-else conditionals, etc.
  • List comprehensions with if conditional is used for filtering while if-else is used when you want to perform different transformations based on the conditions.
  • List comprehensions can be a replacement for map, filter, reduce, and for loop . In most cases list comprehensions is faster than all these methods.
  • If you need to use more than two for loops in the comprehension then it’s better to use the traditional way of creating a list. Because using more than two for loops is not easy to readable.
  • Consider using generator expressions instead of list comprehension if you are dealing with large data and not iterating over the list multiple times.
  • Comprehensions are supposed to be concise , clear , and Pythonic . Don’t write complicated lengthy comprehensions that will defeat the very purpose of using them. 

[1]. https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

Chetan Ambi

Chetan Ambi

Comprehensions and Assignment Expressions

Learn how to make your code more concise using comprehension expressions.

Usage of comprehension expressions

  • Naïve implementation
  • Implementation with comprehension expressions
  • Implementation with assignment expressions
  • Points to remember

Comprehension expressions are usually a more concise way of writing code, and in general, code written this way tends to be easier to read. If we need to do some transformations on the data we're collecting, using a comprehension might lead to more complicated code. In these cases, writing a simple for loop should be preferred instead.

There is, however, one last resort we could apply to try to salvage the situation: assignment expressions. In this lesson, we discuss these alternatives.

The use of comprehensions is recommended for creating data structures in a single instruction, instead of multiple operations. For example, if we wanted to create a list with calculations over some numbers in it, we might try writing it like this:

Get hands-on with 1200+ tech skills courses.

Python Tutorial

File handling, python modules, python numpy, python pandas, python matplotlib, python scipy, machine learning, python mysql, python mongodb, python reference, module reference, python how to, python examples, python - list comprehension, list comprehension.

List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.

Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name.

Without list comprehension you will have to write a for statement with a conditional test inside:

With list comprehension you can do all that with only one line of code:

Advertisement

The return value is a new list, leaving the old list unchanged.

The condition is like a filter that only accepts the items that valuate to True .

Only accept items that are not "apple":

The condition if x != "apple"  will return True for all elements other than "apple", making the new list contain all fruits except "apple".

The condition is optional and can be omitted:

With no if statement:

The iterable can be any iterable object, like a list, tuple, set etc.

You can use the range() function to create an iterable:

Same example, but with a condition:

Accept only numbers lower than 5:

The expression is the current item in the iteration, but it is also the outcome, which you can manipulate before it ends up like a list item in the new list:

Set the values in the new list to upper case:

You can set the outcome to whatever you like:

Set all values in the new list to 'hello':

The expression can also contain conditions, not like a filter, but as a way to manipulate the outcome:

Return "orange" instead of "banana":

The expression in the example above says:

"Return the item if it is not banana, if it is banana return orange".

Get Certified

COLOR PICKER

colorpicker

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected]

Top Tutorials

Top references, top examples, get certified.

Python List Comprehension

A comprehension is a compact way of creating a Python data structure from iterators. With comprehensions, you can combine loops and conditional tests with a less verbose syntax.

Comprehension is considered more Pythonic and often useful in a variety of scenarios.

What is List Comprehension?

List comprehension sounds complex but it really isn’t.

List comprehension is a way to build a new list by applying an expression to each item in an iterable.

It saves you having to write several lines of code, and keeps the readability of your code neat.

Basic Example

Suppose you want to create a list of all integer square numbers from 0 to 4. You could build that list by appending one item at a time to an empty list:

Or, you could just use an iterator and the range() function:

Here both approaches produce the same result. However, a more Pythonic way to build a list is by using a list comprehension.

The general syntax for a list comprehension is:

Python List Comprehension Syntax

Here’s how a list comprehension would build the above list:

In the example above, list comprehension has two parts.

list comprehension example

The first part collects the results of an expression on each iteration and uses them to fill out a new list.

The second part is exactly the same as the for loop, where you tell Python which iterable to work on. Every time the loop goes over the iterable, Python will assign each individual element to a variable x.

More Examples

Below are few examples of list comprehension.

List comprehensions can iterate over any type of iterable such as lists, strings, files, ranges, and anything else that supports the iteration protocol .

Here’s a simple list comprehension that uses string as an iterable.

Following example applies abs() function to all the elements in a list.

Following example calls a built-in method strip() on each element in a list.

Following example creates a list of (number, square) tuples. Please note that, if a list comprehension is used to construct a list of tuples, the tuple values must be enclosed in parentheses.

Here’s why you should use list comprehension more often:

  • List comprehensions are more concise to write and hence they turn out to be very useful in many contexts.
  • Since a list comprehension is an expression, you can use it wherever you need an expression (e.g. as an argument to a function, in a return statement).
  • List comprehensions run substantially faster than manual for loop statements (roughly twice as fast). It offers a major performance advantage especially for larger data sets.

List Comprehension with if Clause

A list comprehension may have an optional associated if clause to filter items out of the result.

Iterable’s items are skipped for which the if clause is not true.

Python List Comprehension If Clause Syntax

This list comprehension is the same as a for loop that contains an if statement :

Nested List Comprehensions

The initial expression in a list comprehension can be any expression, including another list comprehension.

Python Nested List Comprehension Syntax

For example, here’s a simple list comprehension that flattens a nested list into a single list of items.

Here’s another list comprehension that transposes rows and columns.

List Comprehension vs map() + lambda

When all you’re doing is calling an already-defined function on each element, map(f, L) is a little faster than the corresponding list comprehension [f(x) for x in L] . Following example collects the ASCII codes of all characters in an entire string.

However, when evaluating any other expression, [some_expr for x in L] is faster and clearer than map(lambda x: some_expr, L) , because the map incurs an extra function call for each element. Following example creates a list of all integer square numbers.

List Comprehension vs filter() + lambda

List comprehension with if clause can be thought of as analogous to the filter() function as they both skip an iterable’s items for which the if clause is not true. Following example filters a list to exclude odd numbers.

As with map() function, filter() is slightly faster if you are using a built-in function.

List Comprehensions and Variable Scope

In Python 2, the iteration variables defined within a list comprehension remain defined even after the list comprehension is executed.

For example, in [x for x in L] , the iteration variable x overwrites any previously defined value of x and is set to the value of the last item, after the resulting list is created.

so, remember to use variable names that won’t conflict with names of other local variables you have.

Fortunately, this is not the case in Python 3 where the iteration variable remains private, so you need not worry.

Cookie Policy

We use cookies to operate this website, improve usability, personalize your experience, and improve our marketing. Privacy Policy .

By clicking "Accept" or further use of this website, you agree to allow cookies.

  • Data Science
  • Data Analytics
  • Machine Learning

alfie-grace-headshot-square2.jpg

Python List Comprehension: single, multiple, nested, & more

The general syntax for list comprehension in Python is:

Quick Example

We've got a list of numbers called num_list , as follows:

Using list comprehension , we'd like to append any values greater than ten to a new list. We can do this as follows:

This solution essentially follows the same process as using a for loop to do the job, but using list comprehension can often be a neater and more efficient technique. The example below shows how we could create our new list using a for loop.

Using list comprehension instead of a for loop, we've managed to pack four lines of code into one clean statement.

In this article, we'll first look at the different ways to use list comprehensions to generate new lists. Then we'll see what the benefits of using list comprehensions are. Finally, we'll see how we can tackle multiple list comprehensions .

How list comprehension works

A list comprehension works by translating values from one list into another by placing a for statement inside a pair of brackets, formally called a generator expression .

A generator is an iterable object, which yields a range of values. Let's consider the following example, where for num in num_list is our generator and num is the yield.

In this case, Python has iterated through each item in num_list , temporarily storing the values inside of the num variable. We haven't added any conditions to the list comprehension, so all values are stored in the new list.

Conditional statements in list comprehensions

Let's try adding in an if statement so that the comprehension only adds numbers greater than four:

The image below represents the process followed in the above list comprehension:

comprehension assignment python

We could even add in another condition to omit numbers smaller than eight. Here, we can use and inside of a list comprehension:

But we could also write this without and as:

When using conditionals, Python checks whether our if statement returns True or False for each yield. When the if statement returns True , the yield is appended to the new list.

Adding functionality to list comprehensions

List comprehensions aren't just limited to filtering out unwanted list values, but we can also use them to apply functionality to the appended values. For example, let's say we'd like to create a list that contains squared values from the original list:

We can also combine any added functionality with comparison operators. We've got a lot of use out of num_list , so let's switch it up and start using a different list for our examples:

In the above example, our list comprehension has squared any values in alternative_list that fall between thirty and fifty. To help demonstrate what's happening above, see the diagram below:

comprehension assignment python

Using comparison operators

List comprehension also works with or , in and not .

Like in the example above using and , we can also use or :

Using in , we can check other lists as well:

Likewise, not in is also possible:

Lastly, we can use if statements before generator expressions within a list comprehension. By doing this, we can tell Python how to treat different values:

The example above stores values in our new list if they are greater than forty; this is covered by num if num > 40 . Python stores zero in their place for values that aren't greater than forty, as instructed by else 0 . See the image below for a visual representation of what's happening:

comprehension assignment python

Multiple List Comprehension

Naturally, you may want to use a list comprehension with two lists at the same time. The following examples demonstrate different use cases for multiple list comprehension.

Flattening lists

The following synax is the most common version of multiple list comprehension, which we'll use to flatten a list of lists:

The order of the loops in this style of list comprehension is somewhat counter-intuitive and difficult to remember, so be prepared to look it up again in the future! Regardless, the syntax for flattening lists is helpful for other problems that would require checking two lists for values.

Nested lists

We can use multiple list comprehension when nested lists are involved. Let's say we've got a list of lists populated with string-type values. If we'd like to convert these values from string-type to integer-type, we could do this using multiple list comprehensions as follows:

Readability

The problem with using multiple list comprehensions is that they can be hard to read, making life more difficult for other developers and yourself in the future. To demonstrate this, here's how the first solution looks when combining a list comprehension with a for loop:

Our hybrid solution isn't as sleek to look at, but it's also easier to pick apart and figure out what's happening behind the scenes. There's no limit on how deep multiple list comprehensions can go. If list_of_lists had more lists nested within its nested lists, we could do our integer conversion as follows:

As the example shows, our multiple list comprehensions have now become very difficult to read. It's generally agreed that multiple list comprehensions shouldn't go any deeper than two levels ; otherwise, it could heavily sacrifice readability. To prove the point, here's how we could use for loops instead to solve the problem above:

Speed Test: List Comprehension vs. for loop

When working with lists in Python, you'll likely often find yourself in situations where you'll need to translate values from one list to another based on specific criteria.

Generally, if you're working with small datasets, then using for loops instead of list comprehensions isn't the end of the world. However, as the sizes of your datasets start to increase, you'll notice that working through lists one item at a time can take a long time.

Let's generate a list of ten thousand random numbers, ranging in value from one to a million, and store this as num_list . We can then use a for loop and a list comprehension to generate a new list containing the num_list values greater than half a million. Finally, using %timeit , we can compare the speed of the two approaches:

The list comprehension solution runs twice as fast, so not only does it use less code, but it's also much quicker. With that in mind, it's also worth noting that for loops can be much more readable in certain situations, such as when using multiple list comprehensions.

Ultimately, if you're in a position where multiple list comprehensions are required, it's up to you if you'd prefer to prioritize performance over readability.

List comprehensions are an excellent tool for generating new lists based on your requirements. They're much faster than using a for loop and have the added benefit of making your code look neat and professional.

For situations where you're working with nested lists, multiple list comprehensions are also available to you. The concept of using comprehensions may seem a little complex at first, but once you've wrapped your head around them, you'll never look back!

Get updates in your inbox

Join over 7,500 data science learners.

Recent articles:

The 6 best courses to actually learn python in 2024, best course deals for black friday and cyber monday 2024, sigmoid function, dot product, meet the authors.

alfie-grace-headshot-square2.jpg

Alfie graduated with a Master's degree in Mechanical Engineering from University College London. He's currently working as Data Scientist at Square Enix. Find him on  LinkedIn .

Brendan Martin

Back to blog index

  • Free Python 3 Tutorial
  • Control Flow
  • Exception Handling
  • Python Programs
  • Python Projects
  • Python Interview Questions
  • Python Database
  • Data Science With Python
  • Machine Learning with Python
  • Find size of a list in Python
  • Nested List Comprehensions in Python
  • Iterate over a dictionary in Python
  • Why import star in Python is a bad idea
  • How does Python dict.keys() Return a List and a Set?
  • 10 Interesting Python Cool Tricks
  • Mutable vs Immutable Objects in Python
  • How to Minimize Python Script Execution Time?
  • Namespaces and Scope in Python
  • What is the Python Global Interpreter Lock (GIL)
  • Getting started with Jupyter Notebook | Python
  • Python Create Dictionary from List with Default Values
  • Python Convert Set To List Without Changing Order
  • Python Filter List of Dictionaries Based on Key Value
  • Absolute and Relative Imports in Python
  • Extract Dictionary Values as a Python List
  • How to create list of dictionary in Python
  • Appending a dictionary to a list in Python
  • How To Use Lists As Stacks And Queues In Python

Comprehensions in Python

Comprehensions in Python provide us with a short and concise way to construct new sequences (such as lists, sets, dictionaries, etc.) using previously defined sequences. Python supports the following 4 types of comprehension:

List Comprehensions

Dictionary comprehensions, set comprehensions, generator comprehensions.

List Comprehensions provide an elegant way to create new lists. The following is the basic structure of list comprehension:

Syntax: output_list = [ output_exp for var in input_list if ( var satisfies this condition)]

Note that list comprehension may or may not contain an if condition. List comprehensions can contain multiple

Example 1: Generating an Even list WITHOUT using List comprehensions

Suppose we want to create an output list that contains only the even numbers which are present in the input list. Let’s see how to do this using loops, list comprehension, and list comprehension, and decide which method suits you better.

Example 2: Generating Even list using List comprehensions

Here we use the list comprehensions in Python. It creates a new list named list_using_comp by iterating through each element var in the input_list . Elements are included in the new list only if they satisfy the condition, which checks if the element is even. As a result, the output list will contain all even numbers.

Example 1: Squaring Number WITHOUT using List comprehensions

Suppose we want to create an output list which contains squares of all the numbers from 1 to 9. Let’s see how to do this using for loops and list comprehension.

Example 2: Squaring Number using List comprehensions

In This we use list comprehension to generate a new list. It iterates through the numbers in the range from 1 to 9 (inclusive). For each number var , it calculates the square of the number using the expression and adds the result to the new list. The printed output will contain the squares of numbers from 1 to 9.

Extending the idea of list comprehensions, we can also create a dictionary using dictionary comprehensions. The basic structure of a dictionary comprehension looks like below.

output_dict = {key:value for (key, value) in iterable if (key, value satisfy this condition)}

Example 1: Generating odd number with their cube values without using dictionary comprehension

Suppose we want to create an output dictionary which contains only the odd numbers that are present in the input list as keys and their cubes as values. Let’s see how to do this using for loops and dictionary comprehension.

Example 2: Generating odd number with their cube values with using dictionary comprehension

We are using dictionary comprehension in Python. It initializes an list containing numbers from 1 to 7. It then constructs a new dictionary using dictionary comprehension. For each odd number var in the list , it calculates the cube of the number and assigns the result as the value to the key var in the dictionary.

Example 1: Mapping states with their capitals without Using dictionary comprehension

Given two lists containing the names of states and their corresponding capitals, construct a dictionary which maps the states with their respective capitals. Let’s see how to do this using for loops and dictionary comprehension.

Example 2: Mapping states with their capitals with using dictionary comprehension

Here we will use dictionary comprehension to initializes two lists, state and capital , containing corresponding pairs of states and their capitals. It iterates through the pairs of state and capital using the zip() function, and for each pair, it creates a key-value pair in the dictionary. The key is taken from the state list, and the value is taken from the capital list. Finally, the printed output will contain the mapping of states to their capitals.

Set comprehensions are pretty similar to list comprehensions. The only difference between them is that set comprehensions use curly brackets { }

Let’s look at the following example to understand set comprehensions.

Example 1 : Checking Even number Without using set comprehension

Suppose we want to create an output set which contains only the even numbers that are present in the input list. Note that set will discard all the duplicate values. Let’s see how we can do this using for loops and set comprehension.

Example 2: Checking Even number using set comprehension

We will use set comprehension to initializes a list with integer values. The code then creates a new set using set comprehension. It iterates through the elements of the input_list , and for each element, it checks whether it’s even. If the condition is met, the element is added to the set. The printed output which will contain unique even numbers from the list.

Generator Comprehensions are very similar to list comprehensions. One difference between them is that generator comprehensions use circular brackets whereas list comprehensions use square brackets. The major difference between them is that generators don’t allocate memory for the whole list. Instead, they generate each value one by one which is why they are memory efficient. Let’s look at the following example to understand generator comprehension:

Please Login to comment...

Similar reads.

  • python-basics
  • python-dict
  • python-list
  • Technical Scripter 2018
  • Technical Scripter
  • What are Tiktok AI Avatars?
  • Poe Introduces A Price-per-message Revenue Model For AI Bot Creators
  • Truecaller For Web Now Available For Android Users In India
  • Google Introduces New AI-powered Vids App
  • 30 OOPs Interview Questions and Answers (2024)

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

comprehension assignment python

Analytics Insight

Harvard’s Web Programming with Python and Java Script Course

Avatar photo

Harvard’s course on Web Programming with Python and JavaScript offers an in-depth exploration of web app design

Harvard’s Web Programming with Python and JavaScript course stands as a beacon of excellence in the realm of online education, offering students a comprehensive and immersive journey into the world of web development. Developed by the esteemed faculty at Harvard University , this course provides a solid foundation in two essential programming languages: Python and JavaScript .

At the heart of this course lies a commitment to quality instruction and hands-on learning. Through a series of engaging lectures, interactive exercises, and real-world projects, students are guided through the fundamentals of web programming , from basic syntax to advanced concepts. The course covers a wide range of topics, including HTML, CSS, Flask, Django, Node.js, and React.js, empowering students to build dynamic and responsive web applications from scratch.

One of the key strengths of Harvard’s Web Programming course is its emphasis on practical application. Rather than relying solely on theoretical concepts, students are encouraged to roll up their sleeves and dive into coding right from the start. By working on a variety of coding assignments and projects, students gain valuable hands-on experience and develop the skills needed to tackle real-world challenges in web development.

Moreover, Harvard’s course structure fosters a collaborative learning environment, where students can engage with their peers, ask questions, and receive feedback from instructors. This interactive approach not only enhances comprehension but also cultivates a sense of community and support among participants.

Another standout feature of Harvard’s Web Programming course is its flexibility and accessibility. Designed with busy schedules in mind, the course is offered online, allowing students to learn at their own pace and from the comfort of their own homes. Whether you’re a complete beginner or an experienced programmer looking to expand your skill set, this course accommodates learners of all levels, providing comprehensive instruction and support every step of the way.

In addition to its educational value, completing Harvard’s Web Programming course can open doors to exciting career opportunities in the field of web development. With the demand for skilled web developers on the rise, possessing proficiency in Python and JavaScript can significantly enhance your employability and earning potential.

Conclusion:

Harvard’s Web Programming with Python and JavaScript course offers a transformative learning experience for aspiring web developers. From its rigorous curriculum to its practical approach to instruction, this course equips students with the knowledge, skills, and confidence needed to succeed in today’s competitive job market.

You May Also Like

Shiba Inu

Shiba Inu’s Shibarium Reaches 90 Million Transactions Milestone; Decentraland & Borroe Finance Brace for Bullish Momentum

comprehension assignment python

Making 10x Profits with Crypto: Earning Passive Income with Cosmos (ATOM), Scorpion Casino (SCORP), and Stellar (XLM)

Ravi Korlimarla

Ravi Korlimarla: Driving Innovation in Enterprise Software with Cutting-Edge Technologies

comprehension assignment python

How Customer Behaviour and Analytics Shape Our Customer Support Models

AI-logo

Analytics Insight® is an influential platform dedicated to insights, trends, and opinion from the world of data-driven technologies. It monitors developments, recognition, and achievements made by Artificial Intelligence, Big Data and Analytics companies across the globe.

linkedin

  • Select Language:
  • Privacy Policy
  • Content Licensing
  • Terms & Conditions
  • Submit an Interview

Special Editions

  • Dec – Crypto Weekly Vol-1
  • 40 Under 40 Innovators
  • Women In Technology
  • Market Reports
  • AI Glossary
  • Infographics

Latest Issue

Magazine April 2024

Disclaimer: Any financial and crypto market information given on Analytics Insight is written for informational purpose only and is not an investment advice. Conduct your own research by contacting financial experts before making any investment decisions, more information here .

Second Menu

comprehension assignment python

IMAGES

  1. Python List Comprehension (Syntax & Examples)

    comprehension assignment python

  2. Python list comprehension + dictionary comprehension with examples

    comprehension assignment python

  3. Python

    comprehension assignment python

  4. Comprehensive Guide to Python List Comprehensions

    comprehension assignment python

  5. List Comprehension in Python with Example

    comprehension assignment python

  6. List Comprehension in python with example

    comprehension assignment python

VIDEO

  1. List Comprehension In Python In Under A Minute! ✅⚡⏲️ #coding #programming #python #shorts

  2. Assignment

  3. Learn Python List Comprehension

  4. List comprehension

  5. Learn Python List Comprehension

  6. Individual Comprehension Assignment Presentation 1 April 2024

COMMENTS

  1. How can I do assignments in a list comprehension?

    Python 3.8 will introduce Assignment Expressions. It is a new symbol: := that allows assignment in (among other things) comprehensions. This new operator is also known as the walrus operator. It will introduce a lot of potential savings w.r.t. computation/memory, as can be seen from the following snippet of the above linked PEP (formatting ...

  2. PEP 572

    Python Enhancement Proposals. Python » PEP Index » PEP 572; Toggle light / dark / auto colour theme PEP 572 - Assignment Expressions ... However, an assignment expression target name cannot be the same as a for-target name appearing in any comprehension containing the assignment expression. The latter names are local to the comprehension in ...

  3. When to Use a List Comprehension in Python

    Every list comprehension in Python includes three elements: expression is the member itself, a call to a method, or any other valid expression that returns a value. In the example above, the expression number * number is the square of the member value.; member is the object or value in the list or iterable. In the example above, the member value is number.

  4. Python List Comprehension: Tutorial With Examples

    A Python list comprehension is a language construct. It's used to create a Python list based on an existing list. Sounds a little vague, but after a few examples, that 'ah-ha!' moment will follow, trust me. The basic syntax of a list comprehension is: [ <expression> for item in list if <conditional> ]

  5. Python List Comprehension (With Examples)

    Python List Comprehension List comprehension offers a concise way to create a new list based on the values of an existing list. Suppose we have a list of numbers and we desire to create a new list containing the double value of each element in the list.

  6. How To Use Assignment Expressions in Python

    In this tutorial, you used assignment expressions to make compact sections of Python code that assign values to variables inside of if statements, while loops, and list comprehensions. For more information on other assignment expressions, you can view PEP 572 —the document that initially proposed adding assignment expressions to Python.

  7. Comprehensive Guide to Python List Comprehensions

    You can create list comprehensions using the walrus operator := and is also called as "assignment expression". The walrus operator was first introduced in Python 3.8. As mentioned in PEP 572, it was introduced to speedup the coding by writing short code. ... Apart from list comprehensions, Python also has set comprehensions, dictionary ...

  8. Python List Comprehension

    Learn how Python list comprehension can create powerful functionality within a single line of code and create a new list based on the values of an iterable. ... Assignment to multiple variables. List comprehensions can be used to assign values to multiple variables simultaneously. In the example below, we save both the number and its square as ...

  9. Comprehensions and Assignment Expressions

    Comprehensions and Assignment Expressions. Learn how to make your code more concise using comprehension expressions. Comprehension expressions are usually a more concise way of writing code, and in general, code written this way tends to be easier to read. If we need to do some transformations on the data we're collecting, using a comprehension ...

  10. Understanding List Comprehensions in Python 3

    Introduction. List comprehensions offer a succinct way to create lists based on existing lists. When using list comprehensions, lists can be built by leveraging any iterable, including strings and tuples. Syntactically, list comprehensions consist of an iterable containing an expression followed by a for clause.

  11. Python

    List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. Example: Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name. Without list comprehension you will have to write a for statement with a conditional test inside:

  12. Python List Comprehension

    However, a more Pythonic way to build a list is by using a list comprehension. The general syntax for a list comprehension is: Here's how a list comprehension would build the above list: L = [x**2 for x in range(5)] print(L) # Prints [0, 1, 4, 9, 16] In the example above, list comprehension has two parts.

  13. Python

    Now, list comprehension in Python does the same task and also makes the program more simple. List Comprehensions translate the traditional iteration approach using for loop into a simple formula hence making them easy to use. Below is the approach to iterate through a list, string, tuple, etc. using list comprehension in Python.

  14. Python List Comprehension: single, multiple, nested, & more

    We've got a list of numbers called num_list, as follows: num_list = [4, 11, 2, 19, 7, 6, 25, 12] Learn Data Science with. Using list comprehension, we'd like to append any values greater than ten to a new list. We can do this as follows: new_list = [num for num in num_list if num > 10] new_list. Learn Data Science with.

  15. Comprehensions in Python

    We are using dictionary comprehension in Python. It initializes an list containing numbers from 1 to 7. It then constructs a new dictionary using dictionary comprehension. For each odd number var in the list, it calculates the cube of the number and assigns the result as the value to the key var in the dictionary.

  16. The Walrus Operator: Python 3.8 Assignment Expressions

    Each new version of Python adds new features to the language. For Python 3.8, the biggest change is the addition of assignment expressions.Specifically, the := operator gives you a new syntax for assigning variables in the middle of expressions. This operator is colloquially known as the walrus operator.. This tutorial is an in-depth introduction to the walrus operator.

  17. Python's Assignment Operator: Write Robust Assignments

    Here, variable represents a generic Python variable, while expression represents any Python object that you can provide as a concrete value—also known as a literal—or an expression that evaluates to a value. To execute an assignment statement like the above, Python runs the following steps: Evaluate the right-hand expression to produce a concrete value or object.

  18. Assignments in python list comprehension

    Assignments in python list comprehension. Ask Question Asked 2 years, 2 months ago. Modified 2 months ago. Viewed 231 times 3 I am looking for a way to do assignments in a list comprehension. I would like to rewrite something like the following piece of code into a list comprehension. I have this "costly" function: ...

  19. 5 Common Python Gotchas (And How To Avoid Them)

    Its elegant syntax, however, is not immune to quirks that can surprise even experienced Python developers. And understanding these is essential for writing bug-free code—or pain-free debugging if you will. This tutorial explores some of these gotchas: mutable defaults, variable scope in loops and comprehensions, tuple assignment, and more.

  20. Python List Comprehension Is Not Just Syntactic Sugar

    Image created by the author. The first major difference will be the scope of the variables. From the above bytecode, this refers to. LOAD_NAME in the for-loop v.s. LOAD_FAST in the list comprehension; STORE_NAME in the for-loop v.s. STORE_FAST in the list comprehension; To be clear, the variable i in the for-loop version is in the global scope, whereas the variable i in the list comprehension ...

  21. python

    which can be simplified slightly to. if s not in all_freq: all_freq[s] = 0. all_freq[s] += 1. which can be replaced entirely with. Correct. Assignments with =, +=, etc, are statements, not expressions, and you need expressions inside a list comprehension. (We won't get into assignment expressions using := here.)

  22. Harvard's Web Programming with Python and Java Script Course

    Harvard's course on Web Programming with Python and JavaScript offers an in-depth exploration of web app design. Harvard's Web Programming with Python and JavaScript course stands as a beacon of excellence in the realm of online education, offering students a comprehensive and immersive journey into the world of web development. Developed by the esteemed faculty at Harvard University, this ...

  23. python

    I am trying to assign variables 'a' and 'b' as list, with list comprehension, but using tuple as assignment, the point is I don't want to use this: ... how to get tuples from lists using list comprehension in python. 1. List comprehension with list and list of tuples. 0. Comprehension with a Tuple and List. 3.