Python List extend()

Python List extend()

The extend() extends the list by adding all items of a list (passed as an argument) to the end.

The syntax of extend() method is:


Here, the elements of list2 are added to the end of list1.

extend() Parameters

As mentioned, the extend() method takes a single argument (a list) and adds it to the end.

If you need to add elements of other native datatypes (like tuple and set) to the list, you can simply use:

# add elements of a tuple to list

or even easier


Return Value from extend()

The extend() method only modifies the original list. It doesn't return any value.

Example 1: Using extend() Method

# language list
language = ['French', 'English', 'German']

# another list of language
language1 = ['Spanish', 'Portuguese']


# Extended List
print('Language List: ', language)

When you run the program, the output will be:

Language List:  ['French', 'English', 'German', 'Spanish', 'Portuguese']

Example 2: Add Elements of Tuple and Set to List

# language list
language = ['French', 'English', 'German']

# language tuple
language_tuple = ('Spanish', 'Portuguese')

# language set
language_set = {'Chinese', 'Japanese'}

# appending element of language tuple

print('New Language List: ', language)

# appending element of language set

print('Newest Language List: ', language)

When you run the program, the output will be:

New Language List:  ['French', 'English', 'German', 'Spanish', 'Portuguese']
Newest Language List:  ['French', 'English', 'German', 'Spanish', 'Portuguese', 'Japanese', 'Chinese']

The native datatypes like tuple and set passed to extend() method is automatically converted to list. And, the elements of the list are appended to the end.

You can also add items of a list to another list using + or += operator. For example,

a = [1, 2]
b = [3, 4]

a += b

# Output: a = [1, 2, 3, 4]
print('a = ', a)