What is the difference between append() and extend() methods in Python?

In This blog, we are going to learn, What is the difference between append() and extend() methods in python?What is the difference between append() and extend() methods in Python?

Difference between Append and Extend in Python

List: are like an array of dynamic size, declared in another programming language such as vector in C++ and JAVA. Lists need not be homogeneous always which makes it the most powerful tool in Python.

Append

It adds an element at the end of the list. The argument we pass in the append() function is added as a single element at the end of the list, and the length of the list will increase by 1.

Syntax:

list_name.append(element)

The "element" can be an integer, tuple, string or another list.

For Example:-

l = ['test']
 

l.append('to')
l.append('test')
 

print(l)

Output:

['test', 'to', 'test']

Extend

The extend() method concatenates the first list with another list (or another iterable). This will increase the length of the list by the number of elements of the iterable are passed as an argument.

Syntax

list_name.extend(iterable)

For Example:

list = ['The', 'list']  
   
# Using the method  
list.extend(['is', 'an', 'example'])  
list.extend('hello')  
   
 
print ("The extended elements are: ", list)     



Output:
The extended elements  are:  ['The', 'list', 'is', 'an', 'example', 'h', 'e', 'l', 'l', 'o']

Thanks for following and reading this blog.

expoundsofttechsolution
What is SaaS? Software as a Service