top of page

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()

Takes one Argument and it can be an iterator or number data types (int, float etc.)

Takes one argument and it has to be an iterator (int, float, book etc., are not allowed)

Adds its argument as a single element to the end of the list, so the length of the list will always increase by one after the append

Iterator over its argument adding each element to the end of the list, so length of list increases by number of element in the argument

Append with a dictionary as an argument will add the entire dictionary to the end of the list

Extend with dictionary as an argument will add the keys of dictionary to the end of the list iteratively (value are not added)

Time Complexity is O(1)

Time Complexity is O(k) where k is the number of element in the argument.



The Tech Platform

0 comments

Recent Posts

See All
bottom of page