Difference Between List's append() and extend() Methods in Python

Python List's append() Vs. extend() Methods: In this tutorial, we will learn about the append() and extend() methods and the difference between them. By IncludeHelp Last updated : June 20, 2023

Python programming supports many inbuilt methods to work with lists. There are a variety of methods that can be used for inserting and extending the current list with element(s) or list as an element. Here, we will be learning about two popular methods append() and extend() with the help of examples and their difference.

List append() Method

The append() is an inbuilt method of the list class that is used to add an element/item to the list. The append() adds the element at the end. The method is called with this list and accepts an element to be added.

Syntax

The following is the syntax of append() method:

list.append(element)

Example 1

# declaring a list
list1 = [10, 20, 30, 40, 50]

# printing the list before appending
print("list1: ", list1)
print("len(list1): ", len(list1))

# appending an element to the list
list1.append(60)

# printing the list after  appending
print("list1: ", list1)
print("len(list1) ", len(list1))

Output

list1:  [10, 20, 30, 40, 50]
len(list1):  5
list1:  [10, 20, 30, 40, 50, 60]
len(list1)  6

Example 2

# declaring a list
list1 = [10, 20, 30, 40, 50]

# printing the list before appending
print("list1: ", list1)
print("len(list1) ", len(list1))

# appending a list object to the list
list2 = [60, 70, 80]
list1.append(list2)

# printing the list after  appending
print("list1: ", list1)
print("len(list1) ", len(list1))

Output

list1:  [10, 20, 30, 40, 50]
len(list1)  5
list1:  [10, 20, 30, 40, 50, [60, 70, 80]]
len(list1)  6

List extend() Method

The extend() is an inbuilt method of the list class that is used to extend a list, it extends the list by inserting the list of the elements at the end of the current list. The method is called with this list (the current list, in which we have to add the elements), and another list (or any iterable) is supplied as an argument.

Syntax

The following is the syntax of extend() method:

list_name.index(iterable)

Note: If we append a list, then the list appended as an element to the List, thus, the length of the list increased by 1. While, if we extend a list by a list, then the list extended by the given number of elements in the list (which is passed as an argument), thus, the length of the list increased by the given number of elements in the list which is going to be added.

Example

# declaring a list
list1 = [10, 20, 30, 40, 50]

# printing the list before extending
print("list1: ", list1)
print("len(list1) ", len(list1))

# extending the list with another list
list2 = [60, 70, 80]
list1.extend(list2)

# printing the list after  extending
print("list1: ", list1)
print("len(list1) ", len(list1))

Output

list1:  [10, 20, 30, 40, 50]
len(list1)  5
list1:  [10, 20, 30, 40, 50, 60, 70, 80]
len(list1)  8

Difference Between List's append() and extend() Methods

The append() method appends an element to a list at the end and the length grows by one whereas the extend() method extends the list by inserting the list of the elements (or any iterable) at the end and the length grows by number of items in the given iterable.

Program to demonstrate the difference between append() and extend() methods

# Python program to demonstrate the
# difference between append() and
# extend() methods

# Create two lists
list1 = [10, 20, 30]
list2 = [40, 50, 60]

# list to be added
x = [11, 22]

# Print the original lists
print("Original lists...")
print("list1:", list1)
print("list2:", list2)

# Use append() and extend() methods
# and, add x in both of the lists
list1.append(x)
list2.extend(x)

# Print the updated lists
print("Updated lists...")
print("list1:", list1)
print("list2:", list2)

Output

Original lists...
list1: [10, 20, 30]
list2: [40, 50, 60]
Updated lists...
list1: [10, 20, 30, [11, 22]]
list2: [40, 50, 60, 11, 22]

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.