What is the difference between append() and extend() ? DS301222

Extend() method :
The extend() method adds the specified list elements to the end of the current list.

Syntax :
list.extend(iterable)
Ex:
a=[‘sia’,‘ria’,‘kinn’]
b=(1,2,3)
a.extend(b)

Append() method :
The Append() method appends an elements to the end of the list.
Syntax: list.append(elmnt)

Ex:
a=[‘sia’,‘ria’,‘kinn’]
b=(1,2,3)
a.append(b)

The main difference is , in the case of append b would be a nested list
where as in the case of extend it adds elements as extension of that list.
Performance wise append is faster than extend.

2 Likes

Really well explained @kinnukeshiya

1 Like