The Tech Platform

Oct 19, 20211 min

What is the Difference Between Append() and Extend() menthod of list in Python

Append()

The append() method in python adds a single item to the existing list. It doesn't return a new list of items but will modify the original list by adding the item to the end of the list. After executing the method append on the list the size of the list increases by one.

Syntax:

# Adds an object (a number, a string or a
 
# another list) at the end of my_list
 
my_list.append(object)

Extend()

extend() is a built-in Python function that adds the specified list elements (or any iterable) to the end of the current list. For example, the extend() method appends the contents of seq to the list. It extends the list by adding all list elements (passed as an argument) to the end.

Syntax:

# Each element of an iterable gets appended
 
# to my_list
 
my_list.extend(iterable)

Difference Between Append() and Extend()

Append() Extend()

The Tech Platform

www.thetechplatform.com

    0