Concepts / Understanding Lists and Indexing

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.

  • Programming

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.

containscontainscontainsrandomly selectedlook up valuet[1, 2, 3]Index 01Index 1selected2returned elementIndex 12Index 23
How does the selected index identify the exact element that random.choice() returns?

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.

inputselected indexvalue foundPass sequenceto random.choice()Generate index0 through length minus 1Look up elementat that indexOne elementreturned value
What happens step by step from passing a sequence to random.choice() to receiving one selected element?
python
Output
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?

  • 1
  • 2
  • 3
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.

used byreturnsused byreturnst[1, 2, 3]Call 1index 12returned valueCall 2index 23returned value
Given a list and possible random indices, which elements are returned, and what could change on another execution?

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.

FunctionSelection amountReturn formCan a selected element repeat?
random.choice()Exactly one elementThe element itselfA later independent call can select it again
random.choices()One or more elementsA list of selected elementsYes, sampling with replacement allows repetition
returnsreturnsrandom.choice()one selectionelementreturned directlyrandom.choices()one or more selectionslistselected elements
How does selecting one element with random.choice() differ from selecting one or more elements with random.choices()?

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

EASY

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

  1. random.choice() selects exactly one element from a sequence.
  2. It generates a random index from 0 through the sequence length minus 1, then returns the element at that position.
  3. Each call is independent, so repeated calls can return the same element or different elements.
  4. Import the random module before using random.choice().
  5. 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.