Open In App

append() and extend() in Python

Last Updated : 25 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Extend and Append are two Python list methods used to add elements to a list. Although they appear similar, they have different functionalities and use cases. Understanding the differences between the append() and extend() methods is crucial when working with lists in Python. Although both techniques are used to add elements to a list, their behaviors and effects vary. We will examine the distinctions between append() and extend(), how they are used, and when to pick one over the other in this post.

Extend vs Append Python List Methods

In Python, there are two ways to add elements to a list: extend() and append(). However, these two methods serve quite different functions. In append() we add a single element to the end of a list. In extend() we add multiple elements to a list. The supplied element is added as a single item at the end of the initial list by the append() method. When an Iterable is supplied as a parameter, the extend() method adds each element from the Iterable to the list’s end individually. It alters the initial list.

What is Append in Python?

Python’s append() function inserts a single element into an existing list. The element will be added to the end of the old list rather than being returned to a new list. Adds its argument as a single element to the end of a list. The length of the list increases by one. 

 Syntax of Python append()

Syntax:  list.append(item)
Parameters:

  • item: an item to be added at the end of the list, The parameter is mandatory and omitting it can give an error.

Returns: The method doesn’t return any value

Example 1: Here in this example, we have added an item to an existing list using Python.

Python3




my_list = ['geeks', 'for']
my_list.append('geeks')
print my_list


Output

['geeks', 'for', 'geeks']

NOTE: A list is an object. If you append another list onto a list, the parameter list will be a single object at the end of the list. 

Example 2: Here in this example, we have added another list to an existing list using Python.

Python3




my_list = ['geeks', 'for', 'geeks']
another_list = [6, 0, 4, 1]
my_list.append(another_list)
print my_list


Output

['geeks', 'for', 'geeks', [6, 0, 4, 1]]

What is extend() in Python? 

The Python’s List extend() Iterates over its argument and adding each element to the list and extending the list. The length of the list increases by a number of elements in its argument.

 Syntax of Python extend()

Syntax: list.extend(iterable)

Parameters:

  • iterable: Any iterable (list, set, tuple, etc.)

Returns: None

Example 1: Here in this example we are extending a list into a existing list using Python.

Python3




my_list = ['geeks', 'for']
another_list = [6, 0, 4, 1]
my_list.extend(another_list)
print my_list


Output

['geeks', 'for', 6, 0, 4, 1]

NOTE: A string is iterable, so if you extend a list with a string, you’ll append each character as you iterate over the string. 

Example 2: Here in this example we are extending a given item into a existing list using python.

Python3




my_list = ['geeks', 'for', 6, 0, 4, 1]
my_list.extend('geeks')
print my_list


Output

['geeks', 'for', 6, 0, 4, 1, 'g', 'e', 'e', 'k', 's']

Difference Between append() and extend() in Python

Basis for Comparison

Append()

Extend()

Purpose

To add a single entry to the end of a list, use the append() function. To add additional elements or an iterable to the end of a list, use the extend() function.

Input

accepts only one input element.

accepts as input an iterable (such as a list or tuple).

Operation

The append() function adds the full input to the list as a single item.

extend() adds each item to the list independently after iterating through each one in the input.

Efficiency

Since append() only executes one operation, it is typically quicker and more effective than extend().

When adding elements from numerous iterables or with huge inputs, extend() could take longer.

Time complexity

Append has constant time complexity i.e.,O(1)

Extend has a time complexity of O(k). Where k is the length of the list which need to be added.



Similar Reads

Difference between Append, Extend and Insert in Python
Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python. A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their crea
3 min read
Python List Extend vs Append
Python, being a versatile and powerful programming language, provides multiple ways to manipulate lists. Two commonly used methods for adding elements to a list are extend and append. While both serve the purpose of adding elements, they differ in functionality and use cases. In this article, we'll delve into the nuances of Python's extend and appe
4 min read
Python sympy | sieve.extend() method
With the help of sympy.sieve.extend() method, we can grow the sieve to cover all the prime numbers less than equal to a given natural number. Syntax: sieve.extend(N) Parameter: N - It denotes the number up to which the sieve is extended. Returns: The function does not return anything. Example #1: # import sympy from sympy import sieve # Use sieve.e
1 min read
Python | Extend tuples by count of elements in tuple
Sometimes, while working with data, we can have an application in which we need to duplicate tuple elements by the amount of element count. This is very unique application but can occur in certain cases. Let's discuss certain ways in which this task can be performed. Method #1: Using nested loops This is the brute force method by which this task ca
6 min read
Extend Class Method in Python
In Python, when a subclass defines a function that already exists in its superclass in order to add some other functionality in its own way, the function in the subclass is said to be an extended method and the mechanism is known as extending. It is a way by which Python shows Polymorphism. This is similar to overriding in Java and virtual methods
3 min read
Python - Extend consecutive tuples
Given list of tuples, join consecutive tuples. Input : test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)] Output : [(3, 5, 6, 7, 3, 2, 4, 3), (3, 2, 4, 3, 9, 4), (9, 4, 2, 3, 2)] Explanation : Elements joined with their consecutive tuples. Input : test_list = [(3, 5, 6, 7), (3, 2, 4, 3)] Output : [(3, 5, 6, 7, 3, 2, 4, 3)] Explanation : El
3 min read
Python List extend() Method
The Python List extend() method adds items of an iterable (list, tuple, dictionary, etc) at the end of a list. Example C/C++ Code other_cars = ["Maruti", "Tata"] cars.extend(other_cars) print(cars) Output ['Audi', 'BMW', 'Jaguar', 'Maruti', 'Tata']List extend() Syntaxlist_name.extend(iterable) Parameters:iterable: Any iterable (
4 min read
List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
Some of the list methods are mentioned in set 1 below List Methods in Python | Set 1 (in, not in, len(), min(), max()…) More methods are discussed in this article. 1. del[a : b] :- This method deletes all the elements in range starting from index 'a' till 'b' mentioned in arguments. 2. pop() :- This method deletes the element at the position mentio
4 min read
Python | Append at front and remove from rear
Being familiar with the concept of queue, which follows the FIFO rule, i.e first in first out, that suggests a front removal and rear insertion. This has been discussed many times. But sometimes we need to perform the exact opposite of this and we need to perform the append at the front and remove the element from the rear. Let's discuss certain wa
8 min read
Difference Between '+' and 'append' in Python
Using '+' operator to add an element in the list in Python: The use of the '+' operator causes Python to access each element of that first list. When '+' is used a new list is created with space for one more element. Then all the elements from the old list must be copied to the new list and the new element is added at the end of this list. Example:
3 min read
Practice Tags :
three90RightbarBannerImg