Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Create a list of integers from 1 to nE02. Access elements by positive and negative indexE03. Slice a list to get first and last three itemsE

Problem No 3

Slice a list to get first and last three items

Easy

8 minute session

Summary

Return the first three and last three items of a list as a pair of lists; if the list has three or fewer items, return the list for both positions.

Problem statement

Implement slice_first_last_three(lst) that returns a tuple (first_three, last_three) where first_three is the first three items of lst and last_three is the last three items. If lst has length 3 or less, return (lst, lst). This exercises slicing and edge-case handling.

Task

Practice list slicing to extract fixed-size segments and handle short lists gracefully.

Examples

Even-length list

Input

slice_first_last_three([1, 2, 3, 4, 5, 6])

Output

([1, 2, 3], [4, 5, 6])

Explanation

First three are [1,2,3], last three are [4,5,6].

Input format

A single list lst passed to slice_first_last_three(lst).

Output format

A tuple of two lists: (first_three, last_three).

Constraints

Do not modify the original list. If len(lst) <= 3, return (lst, lst).

Samples

Sample input 0

slice_first_last_three([10, 20, 30, 40, 50])

Sample output 0

([10, 20, 30], [30, 40, 50])

Explanation 0

First three are [10,20,30], last three are [30,40,50].

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

02:10 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.