Extending a list in Python
Last Updated :
17 Dec, 2024
In Python, a list is one of the most widely used data structures for storing multiple items in a single variable. Often, we need to extend a list by adding one or more elements, either from another list or other iterable objects. Python provides several ways to achieve this. In this article, we will explore different methods to extend a list.
Using extend()
extend()
method is the most efficient way to add multiple elements to a list. It takes an iterable (like a list, tuple, or set) and appends each of its elements to the existing list.
Python
a = [1, 2, 3]
n = [4, 5, 6]
a.extend(n)
print(a)
Explanation:
- The
extend()
method directly modifies the original list by adding all elements from n
at the end. - It works in-place and avoids creating a new list, making it highly efficient.
Let's explore some more ways of extending a list in Python.
Using the +=
Operator
The +=
operator can be used to extend a list. Internally, it works similarly to the extend()
method but provides a more concise syntax.
Python
a = [1, 2, 3]
n = [4, 5, 6]
a += n
print(a)
Explanation:
- The
+=
operator adds all the elements of 'n'
to the list 'a'
. - It works in-place and avoids creating a new list.
Using slicing
We can use slicing with [:]
to replace or extend the content of a list. It modifies the original list in-place, similar to extend()
.
Python
a = [1, 2, 3]
b = [4, 5, 6]
# Extending list 'a' using slicing
a[len(a):] = b
print(a)
Explanation:
a[len(a):]
represents the slice starting at the end of the list.- Assigning
b
to this slice effectively appends all elements of b
to a
in-place.
NOTE: slicing in Python can be used to insert values at specific positions in a list, for instance, using a[:0],
the values are added at the beginning of the list.
The itertools.chain()
method combines multiple iterables into one continuous sequence, which can be converted to a list or extended into an existing list.
Python
from itertools import chain
a = [1, 2, 3]
n = [4, 5, 6]
a.extend(chain(n))
print(a)
Similar Reads
Appending to 2D List in Python A 2D list in Python is a list of lists where each sublist can hold elements. Appending to a 2D list involves adding either a new sublist or elements to an existing sublist. The simplest way to append a new sublist to a 2D list is by using the append() method.Pythona = [[1, 2], [3, 4]] # Append a new
2 min read
Cloning or Copying a List - Python In Python, lists are mutable, meaning they can be modified after creation. Often, we may need to create a copy of a list to preserve the original data while making changes to the duplicate. Cloning or copying a list can be done in multiple ways, each with its own characteristics. Let's discuss vario
3 min read
Python List extend() Method In Python, extend() method is used to add items from one list to the end of another list. This method modifies the original list by appending all items from the given iterable. Using extend() method is easy and efficient way to merge two lists or add multiple elements at once.Letâs look at a simple
2 min read
Python - Change List Item Lists in Python are mutable meaning their items can be changed after the list is created. Modifying elements in a list is a common task, whether we're replacing an item at a specific index, updating multiple items at once, or using conditions to modify certain elements. This article explores the dif
3 min read
Python - Repeat a element in a List In Python, we often need to add duplicate values to a list, whether for creating repeated patterns, frequency counting, or handling utility cases. In this article, there are various methods to Repeat an element in a List. * operator allows us to repeat an entire list or a list element multiple times
3 min read