Skip to content

Instantly share code, notes, and snippets.

@thuandt
Created January 11, 2013 13:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thuandt/4510611 to your computer and use it in GitHub Desktop.
Save thuandt/4510611 to your computer and use it in GitHub Desktop.
Select a random item from a list/tuple/data stucture in Python

One of the most common tasks that requires random action is selecting one item from a group, be it a character from a string, unicode, or buffer, a byte from a bytearray, or an item from a list, tuple, or xrange. It’s also common to want a sample of more than one item.

Don’t do this when randomly selecting an element

A naive approach to these tasks involves something like the following; to select a single item, you would use randrange (or randint) from the random module, which generates a pseudo-random integer from the range indicated by its arguments:

import random
 
items = ['here', 'are', 'some', 'strings', 'of',
         'which', 'we', 'will', 'select', 'one']
 
rand_item = items[random.randrange(len(items))]

An equally naive way to select multiple items might use random.randrange to generate indexes inside a list comprehension, as in:

rand_items = [items[random.randrange(len(items))]
              for item in range(4)]

These work, but as you should expect if you’ve been writing Python for any length of time, there’s a built-in way of doing it that is briefer and more readable.

Do this instead, when selecting an element

The pythonic way to select a single item from a Python sequence type — that’s any of str, unicode, list, tuple, bytearray, buffer, xrange — is to use random.choice. For example, the last line of our single-item selection would be:

rand_item = random.choice(items)

Much simpler, isn’t it? There’s an equally simple way to select n items from the sequence:

rand_items = random.sample(items, n)

Notes on duplicates

If the sequence contains duplicate values, each one is an independent candidate for selection. If you want to avoid duplicates, random.choice and random.sample can also operate on sets; simply create a set from your sequence using set(items) and proceed as above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment