Python3 List extend() Method

Python3 列表Python3 Lists


Description

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

Syntax

extend() method syntax:

list.extend(seq)

Parameters

  • seq -- A sequence of elements, which can be a list, tuple, set, or dictionary. If it is a dictionary, only the keys will be added as elements to the end of the original list in order.

Return Value

This method has no return value, but it adds new list content to the existing list.

Example

The following example demonstrates how to use the extend() function:

Example

#!/usr/bin/python3 list1 = ['Google', 'Example', 'Taobao'] list2=list(range(5)) # Create a list of 0-4 list1.extend(list2) # Extend the list print ("Extended list:", list1)

The output of the above example is as follows:

扩展后的列表: ['Google', 'Example', 'Taobao', 0, 1, 2, 3, 4]

Different data types:

Example

#!/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)
新列表:  ['French', 'English', 'German', 'Spanish', 'Portuguese']
新列表:  ['French', 'English', 'German', 'Spanish', 'Portuguese', 'Chinese', 'Japanese']

Python3 列表Python3 Lists

Other Extensions