Since Python 3.6 there is a method
choices from the
In : import random In : random.choices( ...: population=[['a','b'], ['b','a'], ['c','b']], ...: weights=[0.2, 0.2, 0.6], ...: k=10 ...: ) Out: [['c', 'b'], ['c', 'b'], ['b', 'a'], ['c', 'b'], ['c', 'b'], ['b', 'a'], ['c', 'b'], ['b', 'a'], ['c', 'b'], ['c', 'b']]
random.choices will sample with replacement, per the docs:
ksized list of elements chosen from the population with replacement.
Note for completeness of answer:
When a sampling unit is drawn from a finite population and is returned
to that population, after its characteristic(s) have been recorded,
before the next unit is drawn, the sampling is said to be “with
replacement”. It basically means each element may be chosen more than
If you need to sample without replacement, then as @ronan-paixão’s brilliant answer states, you can use
replace argument controls such behaviour.