Home > Software design >  List of differing values appended into python list [closed]
List of differing values appended into python list [closed]

Time:09-17

I have

import random
gamma = 1/7
rand1 = random.uniform(1.5, 2.5)
beta = rand1 * gamma
#print(beta)

empty = []
for i in range(100):
    empty.append(beta)

which gives the list empty as the same value of beta 100 times. However, what I wish to have is the list empty consisting of 100 different values of beta. How can this be achieved?

CodePudding user response:

You need to update beta value, or not referring anymore to beta:

empty = []
for i in range(100):
    empty.append(random.uniform(1.5, 2.5)*gamma)

CodePudding user response:

the multiplication of a constant on a list get the effect of expanding the list by itself a number of times equal to the constant

>>> [1,2]*10
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
>>> 

what you want is something like this empty = [gamma * random.uniform(1.5, 2.5) for _ in range(100)] like Dani commented or what Dams show in his/her answers

  • Related