Append vs Extend

Easy ⏱ 8 min 87% acceptance ★★★★☆ 4.3
Write add_items(base, single_item, many_items) that appends single_item to base as one element using .append(), then adds every element of the list many_items individually using .extend(). Return the resulting list. Do not create a new list — mutate and return base.

Examples

Example 1
Input
base = [1, 2], single_item = 3, many_items = [4, 5]
Output
[1, 2, 3, 4, 5]
Explanation

append adds 3 as one item, extend unpacks [4, 5] into individual items.

Constraints

  • 0 <= len(base), len(many_items) <= 10^4

Topics

ListsMutation

Companies

SwiggyZomato

Hints

Hint 1

.append(x) adds x as a single new element.

Hint 2

.extend(iterable) adds each element of iterable separately.

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