Concepts / Understanding Lists and Indexing

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.

  • 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.

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

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?

  1. 1
    List the valid indices

    The three elements occupy indices 0, 1, and 2.

  2. 2
    Use the selected index

    The generated index is 1.

  3. 3
    Look up the element

    The element at index 1 is 2.

Result

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.

Pass sequenceto random.choice()Generate index0 through length minus 1Look up elementat that indexOne elementreturned valueinputselected indexvalue found
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.

Code walkthrough
import random

t = [1, 2, 3]
first = random.choice(t)
second = random.choice(t)
print(first)
print(second)
  1. 1
    The random module is imported before random.choice() is used.
  2. 2
    The list t has elements at indices 0, 1, and 2.
  3. 3
    The first call chooses one valid index and returns the element at that position.
  4. 4
    The second call chooses a fresh valid index and returns the element at that position.
  5. 5
    The 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?

  • 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.

t[1, 2, 3]Call 1index 12returned valueCall 2index 23returned valueused byreturnsused byreturns
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

The key distinction is whether the function returns one element or a list of selected elements.

random.choice()one selectionelementreturned directlyrandom.choices()one or more selectionslistselected elementsreturnsreturns
How does selecting one element with random.choice() differ from selecting one or more elements with random.choices()?

Mistakes to Avoid

  • Common mistake

    Using random.choice() before importing random.

    What it looks like
    selected = random.choice(items)
    Why it fails

    The random module has not been imported, so the name random is not available for this call.

    Do this instead

    Write import random before using random.choice().

  • Common mistake

    Expecting random.choice() to return a list.

    What it looks like
    selected = random.choice(["red", "blue"])
    Why it fails

    random.choice() returns one selected element directly.

    Do this instead

    Use 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 like
    first = random.choice([1, 2, 3]) and second = random.choice([1, 2, 3])
    Why it fails

    Each call independently generates a fresh random index, and the same element can be selected more than once.

    Do this instead

    Treat each call as a new selection from the full sequence.

  • Common mistake

    Tracing the value without tracing its index.

    What it looks like

    For [1, 2, 3], guessing a result without checking the selected position

    Why it fails

    The function selects an index first and then returns the element at that position.

    Do this instead

    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.
Worked example

Checking the Three Possible Indices

Map each possible index for ["north", "south", "west"] to the value returned by random.choice().

  1. 1
    Index 0

    Index 0 refers to the first element, "north".

  2. 2
    Index 1

    Index 1 refers to the second element, "south".

  3. 3
    Index 2

    Index 2 refers to the third element, "west".

  4. 4
    Repeat a call

    A later call can generate any of the same valid indices, so it can return the same word again.

Result

The possible returned elements are "north", "south", and "west".

Key Takeaways

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

Related concepts

More concepts starting with U