Reading Items from a List

~7 mincode completion

Write first_and_last(values) that returns a tuple of the first item and the last item.

For [3, 9, 4, 7] it returns (3, 7). Assume the list has at least one item.

Examples

Picks the ends out of a four-item list

Input
first_and_last([3, 9, 4, 7])
Output
[3, 7]

With two items, first and last are the two items

Input
first_and_last([10, 20])
Output
[10, 20]

With one item, it is both the first and the last

Input
first_and_last([42])
Output
[42, 42]

Hints

Hint 1

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

Hint 2

Watch for this: indexed from one.

Requirements

  • : a non-empty list

  • Return a tuple (first_item, last_item).

Constraints

  • Standard library only, no imports required

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~7 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 first_and_last(values):
    """
    Return the first and last items of a list.

    Args:
        values: a non-empty list

    Returns:
        A tuple (first_item, last_item).
    """
    # 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.