I understand that what you are looking for is a quick assignment of a variable according to a certain condition. There are some ways to do it (I modified the values of your example a little because you were always returning the same list):
Through a function
def valor(j):
if j == 1:
return [{'b': 0.8, 'x': 0.5, 'm': 0.02} for _ in range(100)]
elif j == 2:
return [{'b': 1.8, 'x': 1.5, 'm': 1.02} for _ in range(100)]
elif j == 3:
return [{'b': 2.8, 'x': 2.5, 'm': 2.02} for _ in range(100)]
else:
return None
j = 2
m = valor(j)
print(m)
It is a good alternative because it allows to better verify the condition j
, for example returning a default value if no expected value was chosen.
By using dictionaries
opciones = {1:[{'b': 0.8, 'x': 0.5, 'm': 0.02} for _ in range(100)],
2:[{'b': 1.8, 'x': 1.5, 'm': 1.02} for _ in range(100)],
3:[{'b': 2.8, 'x': 2.5, 'm': 2.02} for _ in range(100)]}
j = 2
m = opciones[j]
print(m)
It is a direct assignment from a dictionary that has as key the valid options, a KeyError
will be emitted if you use any value of j
not contemplated, unless you access the dictionary using the get()
method:
m = opciones.get(j, None)