easy +8 pts

Take n Items

Return a list of the first n items from a list.

Write a function `take_n(items, n)` that takes a list `items` and a non-negative integer `n` and returns a new list containing the first `n` elements of `items`. If `n` is greater than the length of `items`, return the entire list. If `n` is 0, return an empty list. Your function must not modify the original list.

Constraints

`items` is a list of any type. `n` is an integer with `0 <= n <= 1000`. The length of `items` is at most 1000. The returned list should be a copy of the slice, not a reference to the original.

Example

>>> take_n([10, 20, 30, 40], 2)
[10, 20]
>>> take_n(['a', 'b'], 5)
['a', 'b']
>>> take_n([1, 2, 3], 0)
[]
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use list slicing with the colon operator.
Remember that slicing with a stop index larger than the list length automatically stops at the end.
No need to check bounds manually; slicing handles it.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.