CameraIcon
CameraIcon
SearchIcon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

Differentiate between functions of list. append() and extend().

Open in App
Solution

The append() function takes a single element as an argument and appends it to the list. The argument can be any datatype, even a list. The argument will be added as a single element.
>>>list1 = [10, 20, 30]
>>>list1.append(40)


list1 will now be [10, 20, 30, 40]

>>>list1 = [10, 20, 30]
>>>list1.append([40, 50])


Here [40, 50] is a list which will be considered as a single argument, and will be added to the list at index 3. 'list1' will now be [10, 20, 30, [40, 50]]

The extend() function takes any iterable data type (e.g. list, tuple, string, dictionary) as an argument and adds all the elements of the list passed as an argument to the end of the given list. This function can be used to add more than one element in the list in a single statement.

>>> list1 = [2, 4, 5, 6]
>>> list1.extend((2, 3, 4, 5))
>>> print(list1)


OUTPUT:
[2, 4, 5, 6, 2, 3, 4, 5]

flag
Suggest Corrections
thumbs-up
0
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Definition of Function
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon