Home > Software engineering >  Putting Result of Multiplication in a List
Putting Result of Multiplication in a List

Time:11-18

I am trying to put a series of multiplications inside a list, I am using the code below:

listx = []
for i in range (2):
    list = [(3*i)]
    listx.append(list)

The problem is that this will put the two results inside two separate lists inside a lists, I just wants the floats to be inside the first list.

CodePudding user response:

listx = []
for i in range (2):
    listx.append(3*i)

Just use this one. There is no need to create another list for storing the result. You created another list to store the value and appended that list into your listx

CodePudding user response:

You can also use list comprehensions. Basically it's the same with for cycles but it's much shorter and for simpler operations like yours it's easier to read.

listx = [i*3 for i in range(2)]

This should produce a single list with values multiplied by 3 as integers

  • Related