0

I have a list of floats and want to multiply each value by a number. Code will explain it better:

list = [1.0,2.0,3.0]

and now I want to do an operation so that:

list =[1.0,1.0,1.0,2.0,2.0,2.0,3.0,3.0,3.0]

so for example multiply each value by 3 in the way shown. Unfortunately I don't have a clue how to do that.

uitty400
  • 277
  • 1
  • 3
  • 14

2 Answers2

0
for l in list:
    for i in xrange(0,2):
        list.append(l)
Lorcan O'Neill
  • 3,303
  • 1
  • 25
  • 24
0

Using list comprehension:

>>> lst = [1.0, 2.0, 3.0]
>>> [x for x in lst for i in range(3)]
[1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0]

BTW, don't use list as a variable name. It shadows builtin function list.

falsetru
  • 357,413
  • 63
  • 732
  • 636