Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 4

Append an item and extend a list

Easy

6 minute session

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'].

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.

09:08 PM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.