Extend a list in Python

Extending Python List: In this tutorial, we will learn how can we extend a Python list using different methods. Learn with the help of examples of different approaches. By IncludeHelp Last updated : June 25, 2023

There are the following 6 popular different ways to extend a list in Python:

  1. Using plus (+) operator
  2. Using the list.append() method
  3. Using the list.extend() method
  4. Using the list.insert() method
  5. Using list slicing
  6. Using the itertools.chain() method

1. Extend Python list using plus (+) operator

The simple way without using any method for extending a list is to use plus (+) operator. Just provide one or more than one element inside the square brackets [] to the list using the + or += operator.

Example

# list of integers
list1 = [10, 20, 30, 40, 50]

# printing the list
print("Original list:", list1)

# extending list using + operator
list1 += [60]
list1 += [70, 80]

# print the updated list
print("Extended list:", list1)

Output

Original list: [10, 20, 30, 40, 50]
Extended list: [10, 20, 30, 40, 50, 60, 70, 80]

2. Extend Python list using the list.append() method

To extend a list, you can use the list.append() method which is an inbuilt method of the list class that is used to add an element/item to the list.

Example

# list of integers
list1 = [10, 20, 30, 40, 50]

# printing the list
print("Original list:", list1)

# extending list using append() method
list1.append(60)
list1.append(70)
list1.append(80)

# print the updated list
print("Extended list:", list1)

Output

Original list: [10, 20, 30, 40, 50]
Extended list: [10, 20, 30, 40, 50, 60, 70, 80]

3. Extend Python list using the list.extend() method

When you have more than one element, you can use the list.extend() method to extend a list. 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.

Example

# list of integers
list1 = [10, 20, 30, 40, 50]

# printing the list
print("Original list:", list1)

# extending list using extend() method
list1.extend([60, 70, 80])

# print the updated list
print("Extended list:", list1)

Output

Original list: [10, 20, 30, 40, 50]
Extended list: [10, 20, 30, 40, 50, 60, 70, 80]

4. Extend Python list using the list.insert() method

To extend a list, the list.insert() method can also be used. The insert() method is also an inbuilt method of the list class. It accepts two parameters index and element. You need to define the index to insert a specific element. In the below example, we are inserting the element at the end thus we are using the list's length as an index. The length of the list can be found using the len() function.

Example

# list of integers
list1 = [10, 20, 30, 40, 50]

# printing the list
print("Original list:", list1)

# extending list using insert() method
list1.insert(len(list1), 60)
list1.insert(len(list1), 70)
list1.insert(len(list1), 80)

# print the updated list
print("Extended list:", list1)

Output

Original list: [10, 20, 30, 40, 50]
Extended list: [10, 20, 30, 40, 50, 60, 70, 80]

5. Extend Python list using list slicing

Python list slicing can also be used for extending a list. Consider the below example in which we are inserting the elements at the beginning and end.

Example

# list of integers
list1 = [10, 20, 30, 40, 50]

# printing the list
print("Original list:", list1)

# extending list using list slicing
list1[len(list1) :] = [60]  # add 1 element to the end
list1[len(list1) :] = [70, 80]  # add 2 elements to the end

# add 1 element at the beginning
list1[:0] = [5]
# add 2 element at the beginning
list1[:0] = [7, 9]

# print the updated list
print("Extended list:", list1)

Output

Original list: [10, 20, 30, 40, 50]
Extended list: [7, 9, 5, 10, 20, 30, 40, 50, 60, 70, 80]

6. Extend Python list using the itertools.chain() method

You can also use itertools.chain() method to extend a list with one or more than one element (or with a list). To use the chain() method you need to import the itertools module.

Example

# for importing the chain method
from itertools import chain

# list of integers
list1 = [10, 20, 30, 40, 50]

# printing the list
print("Original list:", list1)

# extending list using chain() metod
list1 = list(chain(list1, [60]))  # add 1 element
list1 = list(chain(list1, [70, 80, 90]))  # add multiple elements

# print the updated list
print("Extended list:", list1)

Output

Original list: [10, 20, 30, 40, 50]
Extended list: [10, 20, 30, 40, 50, 60, 70, 80, 90]

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





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