What is List.extend() in Python?

The extend() method adds the specified iterable to the end of the given list.

expand() iterates over an iterable, adding and extending the list with each element of the iterable.

Syntax


list.extend(iterable)

Parameter

The extend() method requires an iterable (list, set, tuple, etc).

Code

The following code shows how to use the extend() method:

Example 1: Adding a tuple to a list

fruits = ['apple', 'banana', 'Avocado']
numbers = (1, 6, 3)
# Using extend() method
fruits.extend(numbers)
print(fruits)

Example 2: Adding a string to another list

A string is iterable. If you extend a list with a string, each character of the string will be appended as you iterate over it.

# Creationg a list
my_list = ['Banana', 'Mango', 7,3]
my_list.extend('Apple') #using extend()
print(my_list)

Free Resources