I use lists to randomly pick an element over many iterations (to create artificial data sets). To change the probability of getting a certain element, I’m repeatedly adding those elements that should have a higher change of being picked, so instead of
JavaScript
x
2
1
fair_list = ["A", "B", "C"]
2
I would do
JavaScript
1
2
1
unfair_list = ["A", "B", "C", "C", "C"]
2
Is there a better way to do this inline? I tried
JavaScript
1
2
1
unfair_list = ["A", "B", 3 * "C"]
2
but this results in
JavaScript
1
2
1
["A", "B", "CCC"]
2
Advertisement
Answer
You can do it in one line actually.
JavaScript
1
2
1
unfiar_list = ["A", "B", *["C"]*3]
2