How can I do assignments in a list comprehension?

I want to use the assignment operator in a list comprehension. How can I do that?

The following code is invalid syntax. I mean to set lst[0] to an empty string '' if it matches pattern:

[ lst[0] = '' for pattern in start_pattern if lst[0] == pattern ]

Thanks!


Solution 1:

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 adapted for SO):

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:

  1. Handle a matched regex

    if (match := pattern.search(data)) is not None:
        # Do something with match
    
  2. A loop that can't be trivially rewritten using 2-arg iter()

    while chunk := file.read(8192):
        process(chunk)
    
  3. Reuse a value that's expensive to compute

    [y := f(x), y**2, y**3]
    
  4. Share a subexpression between a comprehension filter clause and its output

    filtered_data = [y for x in data if (y := f(x)) is not None]
    

This is already available in the recently releases alpha version (not recommended for production systems!). You can find the release schedule for Python 3.8 here.

Solution 2:

It looks like you are confusing list comprehension with looping constructs in Python.

A list comprehension produces -- a list! It does not lend itself to a single assignment in an existing list. (Although you can torture the syntax to do that...)

While it isn't exactly clear what you are trying to do from your code, I think it is more similar to looping over the list (flow control) vs producing a list (list comprehension)

Loop over the list like this:

for pattern in patterns:
   if lst[0] == pattern: lst[0]=''

That is a reasonable way to do this, and is what you would do in C, Pascal, etc. But you can also just test the list for the one value and change it:

if lst[0] in patterns: lst[0] = ''

Or, if you don't know the index:

i=lst.index[pattern]
lst[i]=''

or, if you have a list of lists and want to change each first element of each sublist:

for i, sublst in enumerate(lst):
    if sublst[i][0] in patterns: sublist[i][0]=''

etc, etc, etc.

If you want to apply something to each element of a list, then you can look at using a list comprehension, or map, or one of the many other tools in the Python kit.

Personally, I usually tend to use list comprehensions more for list creation:

 l=[[ x for x in range(5) ] for y in range(4)]  #init a list of lists...

Which is more natural than:

l=[]
for i in range(4):
   l.append([])
   for j in range(5):
      l[i].append(j)      

But to modify that same list of lists, which is more understandable?

This:

l=[['new value' if j==0 else l[i][j] for j in range(len(l[i]))] for i in range(len(l))]

or this:

for i,outter in enumerate(l):
    l[i][0]='new value'               

YMMV

Here is a great tutorial on this.