Slice the Middle Section

Easy ⏱ 8 min 88% acceptance ★★★★☆ 4.2
Write middle_section(items) that returns a new list containing every element of items except the first and last one, using slicing. If items has 2 or fewer elements, return an empty list.

Examples

Example 1
Input
items = [1, 2, 3, 4, 5]
Output
[2, 3, 4]
Explanation

Drop the first and last elements.

Example 2
Input
items = [1, 2]
Output
[]
Explanation

Only two elements remain after dropping both ends.

Constraints

  • 0 <= len(items) <= 10^4

Topics

ListsSlicing

Companies

MetaCognizant

Hints

Hint 1

items[1:-1] slices from index 1 up to (not including) the last index.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.