#๐Ÿ”’ Does list comprehension update the list progressively?

40 messages ยท Page 1 of 1 (latest)

gaunt path
#

Does list comprehension update the list progressively, or does it store the update list in memory or something and then update it after it parses the whole list.

My reason for asking is I have a list of arrays where I define the first one and then i iterate using a for loop applying an operation on the first array to get the second one. I am wondering if I can use list comprehension. Am i able to reference the previously updated element in list comprehension, or does it all update at once at the end?

misty citrusBOT
#

@gaunt path

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

static brook
rich plank
#

I think the answer is easy: if you can't tell what the list comprehension is doing, you shouldn't be using it.

gaunt path
# static brook I don't think I understand what you mean.. do you have some example?
layers = [np.concatenate(([neuralPlayer], boardState)).reshape((layerSizes[0],1))]
for i in range(len(weightMatrices)) :
    layers.append(sigmoidArray(np.matmul(weightMatrices[i], layers[i]) + biasMatrices[i] ))

in this snippet i define the first layer/element in the list, then I use a for loop to do matrix multiplication on the previous layer/element

i am wondering if i can use list comp for this instead of a for loop. for example:

layers = [] * 3
layers[0] = [... whatever]
layers = [matmul(layer[i-1], other matrix[i]) for i in range(len(other matrix)) + 1]
static brook
misty citrusBOT
#

@static brook :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | [101, 102, 103]
002 | [1, 2, 3, 69]
static brook
#

and it doesn't "add" to the existing list

#

You could do: ```py
layers = (
[whatever]

  • [matmul(layer[i-1], other matrix[i]) for i in range(len(other matrix)) + 1]
    )
#

Oh wait, no. Ignore the above messaeg

#

you're referencing the previous item of layers

gaunt path
static brook
#

there's no such thing as a 3-element empty list

#

[] * 3 is the same as []

gaunt path
#

oh

#

can i do [0] * 3?

static brook
#

Yes, that will be the same as [0, 0, 0]

#

but to answer your question, list comprehensions don't have any special behaviour when you assign them to a variable that was holding a list.

#

the last line would be the same as ```py
unrelated = [matmul(layer[i-1], other matrix[i]) for i in range(len(other matrix)) + 1]
layers = unrelated

gaunt path
#

honestly i should have picked a simpler example than the actual problem i have because i am confusing myself

lets say you are given a number and you want a list where each element is the square of the previous starting with the first?

could you do use list comp for that or would you have to use a for loop?

languid belfry
#

well, that would be generally of the form n**2**i I think where i is an integer, but you cannot express n**1 like that

#

!e here's not a list comp, but rather spreading a generator expression into a list

def f(n: int, length: int):
    return [n, *(n**2**i for i in range(length - 1))]

print(f(2, 4))
misty citrusBOT
#

@languid belfry :white_check_mark: Your 3.12 eval job has completed with return code 0.

[2, 2, 4, 16]
languid belfry
#

wait a sec

#

n**2**0 shouldn't that equal n**0 which is 1?

static brook
languid belfry
#

yeah so wth did I do lol

static brook
gaunt path
#

ok so i would have to use a for loop

languid belfry
static brook
#

yeah, a for loop is fine here

gaunt path
#

alr thx

static brook
#

You might want to use zip here to simplify it

languid belfry
#

!e

def f(n: int, length: int):
    return [n, *(n**(2*i) for i in range(1, length))]

print(f(2, 4))
misty citrusBOT
#

@languid belfry :white_check_mark: Your 3.12 eval job has completed with return code 0.

[2, 4, 16, 64]
languid belfry
#

but this is not basing off of last value, it is calculating based on iteration and first value

gaunt path
#

thx for the help, sorry if my question was confusing

#

!close

misty citrusBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.