Understanding Lists and Indexing
A list stores elements at numbered positions called indices. random.choice() uses that position-based structure to select one element: it generates a random index and returns the element at that index.
A Random Pick Has a Position
A list stores elements at numbered positions called indices. random.choice() uses that position-based structure to select one element: it generates a random index and returns the element at that index. The important idea is that the function does not return an abstract random result. It chooses a position first, then retrieves the value stored there.
From Index to Element
Suppose a sequence has three elements. Its valid indices are 0, 1, and 2. random.choice() generates a random integer from 0 through the sequence length minus 1. That integer becomes the selected index. The function then returns the element at that position.
Tracing One Possible Selection
A sequence contains 1 at index 0, 2 at index 1, and 3 at index 2. What does random.choice() return if it generates index 1?
- 1List the valid indices
The three elements occupy indices 0, 1, and 2.
- 2Use the selected index
The generated index is 1.
- 3Look up the element
The element at index 1 is 2.
random.choice() returns 2 for this possible selection.
The Selection Flow
The operation can be traced as a short sequence of steps. First, a sequence is passed to random.choice(). Next, the function generates a valid random index for that sequence. Finally, it returns the element stored at the selected position. The returned value is one element, not a list containing one element.
The output is one of the three elements: red, blue, or green. Which one appears depends on the randomly selected index.Repeated Calls
Every call generates a fresh random index. For a three-element list containing 1, 2, and 3, one call might select index 1 and return 2. A later call might select index 2 and return 3. It could also select index 1 again and return 2 again. The calls are independent, so a previous result does not remove that element from the sequence.
import random
t = [1, 2, 3]
first = random.choice(t)
second = random.choice(t)
print(first)
print(second)- 1The random module is imported before
random.choice()is used. - 2The list t has elements at indices 0, 1, and 2.
- 3The first call chooses one valid index and returns the element at that position.
- 4The second call chooses a fresh valid index and returns the element at that position.
- 5The two printed values may be the same or different.
What do you think happens?
If t is [1, 2, 3] and a call generates index 2, what value does that call return?
Reveal answer
3
Index 2 points to the third element of the list, which is 3. A later call can select a different index or select index 2 again.
Choice Versus Choices
The function names differ by one letter, but their results differ. random.choice() selects exactly one element and returns that element directly. random.choices() selects one or more elements and returns the selected elements as a list. random.choices() can sample with replacement, so the same element can appear more than once in its returned list.
| Function | Selection amount | Return form | Can a selected element repeat? |
|---|---|---|---|
| random.choice() | Exactly one element | The element itself | A later independent call can select it again |
| random.choices() | One or more elements | A list of selected elements | Yes, sampling with replacement allows repetition |
The key distinction is whether the function returns one element or a list of selected elements.
Mistakes to Avoid
Common mistake Using random.choice() before importing random.
What it looks likeselected = random.choice(items)Why it failsThe random module has not been imported, so the name random is not available for this call.
Do this insteadWrite import random before using random.choice().
Common mistake Expecting random.choice() to return a list.
What it looks likeselected = random.choice(["red", "blue"])Why it failsrandom.choice()returns one selected element directly.Do this insteadUse random.choices() when the required result is a list of one or more selected elements.
Common mistake Assuming a later call must choose a different element.
What it looks likefirst = random.choice([1, 2, 3]) and second = random.choice([1, 2, 3])Why it failsEach call independently generates a fresh random index, and the same element can be selected more than once.
Do this insteadTreat each call as a new selection from the full sequence.
Common mistake Tracing the value without tracing its index.
What it looks likeFor [1, 2, 3], guessing a result without checking the selected position
Why it failsThe function selects an index first and then returns the element at that position.
Do this insteadWrite the valid indices beside the sequence, then map the selected index to its element.
Practice the Trace
A sequence contains the elements "north", "south", and "west" in that order. Trace the result of random.choice() for each possible generated index: 0, 1, and 2. Then explain why two calls could return the same word.
Hints
- Start indexing at 0.
- Map each generated index to the element in that position.
- Remember that each call generates a fresh random index.
Checking the Three Possible Indices
Map each possible index for ["north", "south", "west"] to the value returned by random.choice().
- 1Index 0
Index 0 refers to the first element, "north".
- 2Index 1
Index 1 refers to the second element, "south".
- 3Index 2
Index 2 refers to the third element, "west".
- 4Repeat a call
A later call can generate any of the same valid indices, so it can return the same word again.
The possible returned elements are "north", "south", and "west".
Key Takeaways
- 1
random.choice()selects exactly one element from a sequence. - 2It generates a random index from 0 through the sequence length minus 1, then returns the element at that position.
- 3Each call is independent, so repeated calls can return the same element or different elements.
- 4Import the random module before using
random.choice(). - 5
random.choices()returns a list of one or more selected elements and allows sampling with replacement.
At a glance
random.choice()chooses one position and returns the element stored there.- For a sequence of length n, the possible indices run from 0 through n minus 1.
- Independent calls can select the same element repeatedly.
random.choice()returns one element, whilerandom.choices()returns a list of selected elements.