Lesson guide
What this Python exercise practices
Append an item and extend a list is a beginner practice lesson that focuses on lists, iteration, filtering. It is designed to be solved in about 6 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- List values
- Basic indexing
Difficulty and time
- Level
- Beginner
- Estimated time
- 6 minutes
Practice path
Summary
Learn how to add a single item to a list and then extend it with another iterable, returning the modified list.
Problem statement
Write a function append_and_extend(lst, item, to_extend) that appends item to the end of lst and then extends lst with all elements from to_extend (an iterable). The function should modify the provided list and return it. Handle any iterable for to_extend (e.g., list, tuple, range). Do not create and return a new list — modify the original and return it.
Task
Implement a function that appends one element to a list and extends it with another iterable, returning the modified list.
Examples
Append and extend a list
Input
append_and_extend([1, 2], 3, [4, 5])
Output
[1, 2, 3, 4, 5]
Explanation
First append 3 to [1, 2] -> [1, 2, 3], then extend with [4, 5] -> [1, 2, 3, 4, 5].
Input format
A function call: append_and_extend(lst, item, to_extend)
Output format
Return the modified list (the same object after changes).
Constraints
Do not create a new list; perform operations in-place (use append and extend semantics). to_extend can be any iterable.
Samples
Sample input 0
append_and_extend([], 'a', [])
Sample output 0
['a']
Explanation 0
Appending 'a' to an empty list and extending with an empty iterable results in ['a'].
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.