Skip to content
Advertisement

python multiply list elements inside list

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

fair_list = ["A", "B", "C"]

I would do

unfair_list = ["A", "B", "C", "C", "C"]

Is there a better way to do this inline? I tried

unfair_list = ["A", "B", 3 * "C"]

but this results in

["A", "B", "CCC"]

Advertisement

Answer

You can do it in one line actually.

unfiar_list = ["A", "B", *["C"]*3]
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement