random.choice() Function Explained

random.choice() lets you pick one random item from a sequence.

Use it when you want a single random value from something like:

  • a list
  • a tuple
  • a string

It returns the chosen item directly. The sequence must contain at least one item.

import random

colors = ["red", "blue", "green"]
choice = random.choice(colors)
print(choice)

Use random.choice() when you want one random item from a non-empty sequence like a list, tuple, or string.

What random.choice() does

random.choice():

  • Picks one random item from a sequence
  • Works with lists, tuples, and strings
  • Returns the chosen item
  • Raises an error if the sequence is empty

This is useful in small programs such as:

  • picking a random player
  • choosing a quiz question
  • selecting a menu option
  • choosing a random letter or word

If you are new to the random module, see the Python random module overview.

Basic syntax

The basic syntax is:

random.choice(sequence)

You must import the random module first:

import random

Then pass one sequence argument:

import random

numbers = [10, 20, 30]
result = random.choice(numbers)
print(result)

Example output:

20

Your output may be different, because the result is random.

Important points:

  • random.choice() takes one sequence
  • It returns one value
  • It does not return a list

Common examples

Pick a random name from a list

import random

names = ["Ana", "Ben", "Chris", "Dina"]
winner = random.choice(names)

print(winner)

This chooses one name from the list.

Pick a random number from a tuple

import random

values = (5, 10, 15, 20)
number = random.choice(values)

print(number)

This works because tuples are sequences.

Pick a random character from a string

import random

text = "python"
letter = random.choice(text)

print(letter)

This returns one character from the string.

These kinds of examples are common in beginner projects like games, quizzes, and simple scripts.

What counts as a sequence

random.choice() works with sequences.

Valid examples:

  • Lists
  • Tuples
  • Strings

List example

import random

colors = ["red", "blue", "green"]
print(random.choice(colors))

Tuple example

import random

numbers = (1, 2, 3)
print(random.choice(numbers))

String example

import random

word = "apple"
print(random.choice(word))

If you need help with lists, see Python lists explained for beginners.

Sets and dictionaries are not good direct choices here because they are not sequences in this use.

For example, this is not the normal way to use random.choice():

data = {"a", "b", "c"}

If your data is in a set or dictionary, convert it to a list first:

import random

data = {"a", "b", "c"}
item = random.choice(list(data))
print(item)

What happens with an empty sequence

If the sequence is empty, random.choice() raises an error.

Example:

import random

items = []
print(random.choice(items))

This causes an error because there is nothing to choose from.

A simple way to prevent this is to check first:

import random

items = []

if items:
    print(random.choice(items))
else:
    print("The list is empty.")

This works because an empty list is treated as False in an if statement.

You can also check the length:

import random

items = ["apple", "banana"]

if len(items) > 0:
    print(random.choice(items))

Return value

random.choice() returns one item from the sequence.

The type of the result depends on what was chosen.

Examples:

import random

print(random.choice([1, 2, 3]))      # int
print(random.choice(["a", "b", "c"]))  # str
print(random.choice("hello"))        # one-character str

If you choose from a string, the result is a one-character string, not a list and not a longer substring.

When to use random.choice() vs other random tools

Use the right tool for the job:

  • Use random.choice() for one random item
  • Use random.randint() for a random integer in a range
  • Use random.shuffle() to reorder a list
  • Do not use random.choice() when you need multiple unique items

Example of random.choice() for one item:

import random

fruits = ["apple", "banana", "orange"]
print(random.choice(fruits))

Example of random.randint() for a number in a range:

import random

print(random.randint(1, 6))

Example of random.shuffle() to reorder a list:

import random

cards = ["A", "K", "Q", "J"]
random.shuffle(cards)
print(cards)

If you need several unique selections, random.choice() is not the best option because it picks only one item per call and can repeat values.

Common mistakes

Here are some common problems beginners run into with random.choice():

  • Forgetting to import random before calling random.choice()
  • Passing an empty list, tuple, or string
  • Trying to use it directly on data that is not a sequence
  • Expecting more than one item to be returned

Forgetting to import random

This will fail:

colors = ["red", "blue", "green"]
print(random.choice(colors))

Fix it by importing the module:

import random

colors = ["red", "blue", "green"]
print(random.choice(colors))

Passing an empty sequence

This causes an error:

import random

items = []
print(random.choice(items))

Fix it by checking first:

import random

items = []

if items:
    print(random.choice(items))
else:
    print("No items available")

Passing the wrong kind of value

If the value is not a sequence, random.choice() is the wrong tool.

For example, if you have a dictionary:

import random

data = {"name": "Ana", "age": 20}
print(random.choice(list(data)))

This converts the dictionary keys to a list first.

Expecting more than one result

This returns only one item:

import random

numbers = [1, 2, 3, 4]
result = random.choice(numbers)
print(result)

If you need to work with list values more confidently, it also helps to know how to check if a value exists in a list in Python.

Useful debugging checks:

print(sequence)
print(type(sequence))
print(len(sequence))
import random; print(random.choice([1, 2, 3]))

These help you confirm:

  • what value you passed in
  • whether it is the right type
  • whether it is empty

FAQ

Does random.choice() return more than one item?

No. It returns one random item each time you call it.

Can I use random.choice() with a string?

Yes. It returns one random character from the string.

Why does random.choice() fail on an empty list?

Because there is no item to choose. Check that the sequence is not empty first.

Is random.choice() truly random?

It is pseudorandom. It is fine for beginner programs, games, and simple scripts.

See also

Now try using random.choice() in a small practice script, such as a name picker, a simple game, or a menu selector.