PortableGibbon - 2 months ago 5
Python Question

# How to loop a formula into a list

I am new to coding and my spelling is not the best but I need help with something, I am trying to loop a formula to make a sequence that is stored in a list. Here is my code so far;

`````` for i in range(list):
number1 = number63*number99
number2 = number1*number33
number3 =(number2*number34)+(number64*number35)
total = number1 + number2 + number3
``````

don't worry about the variables they are completely irrelevant as they only make sense to me but i just was to know how i could do this.

Is this what you want to do?

``````for i in range(list):
number1 = number63*number99
number2 = number1*number33
number3 =(number2*number34)+(number64*number35)
total += [number1] + [number2] + [number3]
``````

This will make `total` a list which adds the three numbers to it each loop iteration

An alternative is to use `extend`:

``````total.extend([number1, number2, number3])
``````

Please note that for both of these methods, you need to initialize `total` somewhere earlier in your code, e.g.

``````total = []
``````

Additionally, you are using the `range` function wrong. If you want to iterate the length of a list, do `range(0,len(lst))`. Please also note that using the variable name `list` is ill-advised as it is a built-in function in Python.

Source (Stackoverflow)