Open In App

Extending a list in Python (5 different ways)

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn different methods of extending a list in Python.

The list is the widely used Python data structure and it can be extended by adding elements to the list. There are various methods to extend the list in Python which includes using an inbuilt function such as append(), chain() and extend() function and using the ‘+’ operator and list slicing. Let’s see all of them one by one.

1. Using append() function

We can append at the end of the list by using append() function. For appending any single value to the list or appending a list to the list, the syntax stays the same. But we can only append a single value at a time using append() function 

Python3




# Python program to extend a list using append()
 
a = [10, 12, 13, 17]
 
# appending multiple values
a.append(20)
a.append(22)
print(a)


Output:

[10, 12, 13, 17, 20, 22]

Time complexity: O(1)
Auxiliary space: O(1) 

2. Using ‘+’ operator: 

We can add values by using the “+” operator. We can use [] to add any number of values to the list. Adding multiple values can be done by using ‘, ‘ values. 

Python3




# Python program to extend a list using '+'
 
a = [10, 12, 13, 17]
 
# Appending single value
a = a + [20]
 
# append more than one values
a = a + [30, 40]
print(a)


Output:

[10, 12, 13, 17, 20, 30, 40]

Time complexity: O(n), where n is the total number of elements in the list 
Auxiliary space: O(k), where k is the number of appended elements.

3. Using slicing: 

Using slicing in Python, single or multiple values can be added to a list.

a[:0] = [x, y, z…]

Here a is the list in which the values(x, y, z..) are to be added. In this method the values are appended to the front of the list.  

Python3




# Python program to extend a list using 'slicing'
 
# appending multiple value
a =[10, 12, 13, 17]
 
# add 1 number
a[:0] = [30]
 
# add two numbers
a[:0] = [40, 50]
print(a)


Output:

[40, 50, 30, 10, 12, 13, 17]

Time complexity: O(n), where n is the number of elements in the list a.

Auxiliary space: O(k), where k is the number of elements being added to the list using slicing.

4. Using chain(): 

Using chain() iterator function, we can extend a list by the syntax:

list(chain(a, [x, y, z..]))

Here a is the list in which the values(x, y, z..) are to be added. In this method the values are appended to the end of the list. 

Python3




# python program to extend a list using
# "chain" iterator functions
from itertools import *
 
a = [10, 20, 30]
 
# extend a list
print(list(chain(a, [40, 50, 60])))


Output:

[10, 20, 30, 40, 50, 60]

The time complexity of the program is O(n), where n is the total number of elements in the resulting list.

The auxiliary space complexity of the program is O(m), where m is the number of elements in the second list being added to the first list. 

5. Using extend() method

This method iterates over an iterable like string, list, tuple, etc., and adds each element of the iterable at the end of the List, extending the original list.

Python3




# Python program to extend a list using extend()
a = [10, 12, 13, 17]
 
b = [30, 40]
 
a.extend(b)
 
print(a)


Output:

[10, 12, 13, 17, 30, 40]


Last Updated : 08 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads