Python provides two different methods, append() and extend() to extend indexes at runtime. Both the methods have some unique attributes and differences. In this article, we will learn the differences between append() and extend().

So, let’s first begin describing the append method.

Python Append()

The Python Append() method alters the current list by appending a single component to the tail. In simple words, we can say that this method adds a single component to the end of a list.

Syntax for the append() method:

list.append(item)

Program Example:

my_list = [‘byjus’, ‘top’]

#Add ‘byjus’ to the list

my_list.append(‘byjus’)

print my_list

OUTPUT: [‘byjus’, ‘top’, ‘byjus’]

Python extend()

In the case of the Python extend() method, the length or size of the list increases by the number of components available in the iterable.

Syntax for extend() in Python

list.extend(iterable)

Program Example:

# my_list

my_list = [‘byjus’, ‘top’]

# Another list

another_list = [1,2,3,4]

Using extend() method

my_list.extend(another_list)

print my_list

OUTPUT: [‘byjus’, ‘top’, 1,2, 3, 4]

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus, GATE Previous Year Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*