Slicing a List

~8 mincode completion

Write middle_three(values) that returns the items at index 1, 2 and 3 as a new list.

For [10, 20, 30, 40, 50] it returns [20, 30, 40].

Examples

Takes the three middle items of a five-item list

Input
middle_three([10, 20, 30, 40, 50])
Output
[20, 30, 40]

The stop index is excluded, so index 4 is not included

Input
middle_three([0, 1, 2, 3, 4])
Output
[1, 2, 3]

A short list returns whatever exists, without an error

Input
middle_three([1, 2])
Output
[2]

Hints

Hint 1

Work directly with the arguments and return the result rather than printing it.

Hint 2

Watch for this: off by one on stop.

Requirements

  • : a list

  • Return a new list with those items.

Constraints

  • Standard library only, no imports required

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~8 min

8 employers weight this skill

3 quant funds, 2 frontier labs, 2 big tech firms, 1 AI product company. Top match scores 80.

Python
def middle_three(values):
    """
    Return the items at indices 1, 2 and 3.

    Args:
        values: a list

    Returns:
        A new list with those items.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.