random.choice
Pick a random item of a sequence.
Python 3.13
random.choice(seq)Parameters
- seqSequence
- The sequence to pick an item from; it must not be empty.
Return value
One item of the sequence; raises IndexError for an empty sequence.
Examples
Same choice on every run
Python 3.13
import random
random.seed(42)
print(random.choice(['red', 'green', 'blue']))blue
Random character of a string
Python 3.13
import random
random.seed(3)
print(random.choice('aeiou'))e
Details
A set does not work; convert it to a list first. To get a repeatable result, call random.seed(...) first.
