Lists · Class 11 · CBSE · Science Computer Science

Explain the difference between append() and extend() in Python lists, using the following statements: x = [1, 2]; x.append([3, 4]) and y = [1, 2]; y.…

Practice question from Lists (Class 11 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.

medium · Subjective · Q-16891D

Explain the difference between append() and extend() in Python lists, using the following statements: x = [1, 2]; x.append([3, 4]) and y = [1, 2]; y.extend([3, 4]).

Answer: append() adds its argument as a single element, so x becomes [1, 2, [3, 4]]. extend() adds each element of an iterable separately, so y becomes [1, 2, 3, 4].

Solution

append() increases the list length by one because it adds the complete argument as one element. extend() adds the elements of the given iterable individually.

More from Lists

All of Lists

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed