Understanding Lists and Indexing
random.choice() selects one element at random from a sequence by generating a random index and returning the element at that position.
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?
List the valid indices: The three elements occupy indices 0, 1, and 2.
Use the selected index: The generated index is 1.
Look 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)
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
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 |
Mistakes to Avoid
Using random.choice() before importing random.
The random module has not been imported, so the name random is not available for this call.
Fix:
Write import random before using random.choice().Expecting random.choice() to return a list.
random.choice() returns one selected element directly.
Fix:
Use random.choices() when the required result is a list of one or more selected elements.Assuming a later call must choose a different element.
Each call independently generates a fresh random index, and the same element can be selected more than once.
Fix:
Treat each call as a new selection from the full sequence.Tracing the value without tracing its index.
The function selects an index first and then returns the element at that position.
Fix:
Write 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().
Index 0: Index 0 refers to the first element, "north".
Index 1: Index 1 refers to the second element, "south".
Index 2: Index 2 refers to the third element, "west".
Repeat 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
- random.choice() selects exactly one element from a sequence.
- It generates a random index from 0 through the sequence length minus 1, then returns the element at that position.
- Each call is independent, so repeated calls can return the same element or different elements.
- Import the random module before using random.choice().
- random.choices() returns a list of one or more selected elements and allows sampling with replacement.
Key Takeaways
- 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, while random.choices() returns a list of selected elements.