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)
New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources