Home > Software engineering >  Create multiple objects inside forloop
Create multiple objects inside forloop

Time:06-05

Python noob here.

I want to create a bunch of list using a forloop.

They all need to in the form of l1, l2, l3, l4, etc.

I am trying to use the following code and I keep getting error, "SyntaxError: can't assign to function call"

The following is the code and any help will be appreciated.

rets = [1,2,3,4,5,6,7,8,9,10]

for i in range(0, len(rets)-1):
  object("L", str(i)) = [0]*(len(rets)-i)

So the objects need to be list that repeats 0. And the length should decrease by i. For eg, the last object will have length of 1.

I believe the error is related to the name of list but I am unable to fix it.

CodePudding user response:

What you are trying to achieve is creating variable names (L1, L2, etc.), which can’t be done by creating an object like you did, and I’m not aware of any ways to dynamically creating variable names like this outside of PHP

  • Related