Easy Tutorial
❮ Python Install Ref Math Nan ❯

Python3 List extend() Method

Python3 List


Description

The extend() function is used to append another sequence of multiple values to the end of a list (extending the original list with a new list).

Syntax

Syntax for the extend() method:

list.extend(seq)

Parameters

Return Value

This method does not return any value but adds the content of the new list to the existing list.

Example

The following example demonstrates the use of the extend() function:

#!/usr/bin/python3

list1 = ['Google', 'tutorialpro', 'Taobao']
list2 = list(range(5)) # Creates a list from 0 to 4
list1.extend(list2)  # Extends the list
print("Extended list:", list1)

Output of the above example:

Extended list: ['Google', 'tutorialpro', 'Taobao', 0, 1, 2, 3, 4]

Different data types:

#!/usr/bin/python3

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

# Tuple
language_tuple = ('Spanish', 'Portuguese')

# Set
language_set = {'Chinese', 'Japanese'}

# Add tuple elements to the end of the list
language.extend(language_tuple)

print('New list:', language)

# Add set elements to the end of the list
language.extend(language_set)

print('New list:', language)

Output:

New list: ['French', 'English', 'German', 'Spanish', 'Portuguese']
New list: ['French', 'English', 'German', 'Spanish', 'Portuguese', 'Chinese', 'Japanese']

Python3 List

❮ Python Install Ref Math Nan ❯