Problem No 3
Slice a list to get first and last three items
Easy≈ 8 minute session
Lesson guide
What this Python exercise practices
Slice a list to get first and last three items is a beginner practice lesson that focuses on lists, iteration, filtering. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- List values
- Basic indexing
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
Practice path
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].
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.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.