Skip to main content

How to Pick a Random Item with Weights in Python with random.choices

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A prize draw where the sticker comes up far more often than the mug needs more than a plain random pick: each item needs its own chance. random.choices picks from a list and lets you give every item a weight, so some come up more often than others. This guide does it on Python 3.15.2.

Syntax​

Syntax
import random

<picks> = random.choices(<items>, weights=<weights>, k=<how_many>)
PartWhat it means
<items>The list to pick from
weights=<weights>A list with one number per item, in the same order. An item with a higher number is picked more often. Leave it out to give every item the same chance
k=<how_many>How many picks to make. Leave it out to make one
<picks>A list of the picked items. It's a list even when it holds one pick

The Python documentation says random.choices will "Return a k sized list of elements chosen from the population with replacement", and that "If a weights sequence is specified, selections are made according to the relative weights."

The Example​

A stall gives every visitor a prize: a sticker, a pen or a mug. Stickers are cheap and mugs aren't, so the sticker gets a weight of 6, the pen 3 and the mug 1. The script draws one prize, then ten.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

prizes.py
import random

prizes = ["sticker", "pen", "mug"]
chances = [6, 3, 1]

one = random.choices(prizes, weights=chances)
print(one)
print(one[0])

ten = random.choices(prizes, weights=chances, k=10)
print(ten)

The prizes.py script in a macOS-style code window: a prizes list of sticker, pen and mug, a chances list of 6, 3 and 1, one pick with random.choices and its first item, then ten picks with k=10.

chances holds one weight for each prize, in the same order as prizes: 6 for the sticker, 3 for the pen and 1 for the mug. The first call makes one pick, and the second makes ten.

Step 2: Run It​

Terminal
uv run python prizes.py
Output
['sticker']
sticker
['sticker', 'sticker', 'sticker', 'pen', 'sticker', 'sticker', 'sticker', 'sticker', 'sticker', 'sticker']

The terminal after uv run python prizes.py: a list holding sticker, then sticker on its own, then a list of ten picks with nine stickers and one pen.

The first line is a list with one item, because random.choices always returns a list. one[0] takes the item out of it. The ten picks are nine stickers and one pen: the sticker has the highest weight, so it came up most.

Your picks will be different, because they're random. When I ran the script a second time, the ten picks held seven stickers, two pens and a mug. The command works unchanged in PowerShell.

What the Weights Mean​

The weights are compared with each other. They add up to 10 here, so 6, 3 and 1 mean a chance of 6 in 10 for the sticker, 3 in 10 for the pen and 1 in 10 for the mug. Ten picks are too few to show that clearly, so I made 1,000 picks with k=1000 and counted them: 602 stickers, 305 pens and 93 mugs. That's close to 600, 300 and 100, and a little off, as chance always is.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting one item, not a listrandom.choices(prizes, weights=chances) gave ['sticker'], as Step 2 showedTake the item out with [0]: one[0] gave sticker
Giving two weights for three itemsrandom.choices(prizes, weights=[6, 3]) raised ValueError: The number of weights does not match the populationGive one weight per item, in the same order
Setting every weight to 0weights=[0, 0, 0] raised ValueError: Total of weights must be greater than zeroGive at least one item a weight above 0

FAQ​

Do the weights have to add up to 1 or to 100?​

No. The documentation calls them "relative weights": only their sizes compared with each other count. [6, 3, 1], [60, 30, 10] and [0.6, 0.3, 0.1] all give the sticker six times the chance of the mug, and each of them ran without an error.

Can the same item come up twice?​

Yes. The picks are made "with replacement", which means a picked item stays in the list for the next pick. That's why the ten picks in Step 2 hold nine stickers. For picks that never repeat, use random.sample: random.sample(prizes, k=2) gave ['sticker', 'mug'], and asking it for four of the three prizes raised ValueError: Sample larger than population or is negative.

What happens if I leave out the weights?​

Every item gets the same chance. The documentation says: "If neither weights nor cum_weights are specified, selections are made with equal probability." In one run, random.choices(prizes, k=10) gave three pens, four mugs and three stickers: ['pen', 'pen', 'mug', 'mug', 'sticker', 'pen', 'mug', 'mug', 'sticker', 'sticker'].

How is random.choice different?​

random.choice, without the s, returns one item instead of a list, and it has no weights. random.choice(prizes) gave 'sticker'. Use it for one pick with equal chances, and random.choices when you need weights or several picks.

What if the list is empty?​

There's nothing to pick, so random.choices([], k=1) raised IndexError: list index out of range.

Conclusion​

random.choices(prizes, weights=chances) picked one prize and returned it in a list, and k=10 made ten picks at once. The weights 6, 3 and 1 made the sticker the most common pick: 602 of 1,000.

Additional Resources​