Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 List extend() Method

Python3 List extend() Method

Python3 Lists The Python3 list


Description

The extend() function appends multiple values from another sequence to the end of a list at once (expanding the original list with a new list).

Syntax

extend() method syntax:


list.extend(seq)

Parameters

Return Value

This method has no return value, but adds the contents of the new list to the existing list.

Example

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

Example

#!/usr/bin/python3

list1

=

[

'

Google

'

,

'

Runoob

'

,

'

Taobao

'

]

list2

=

list

(

range

(

5

)

)

Create a list of 0-4

list1

.

extend

(

list2

)

Extend the list

print

(

"

The extended list:

"

,

list1

)

The output of the above example is:


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

Different data types:

Example

#!/usr/bin/python3

Language list

language

=

[

'

French

'

,

'

English

'

,

'

German

'

]

Tuple

language_tuple

=

(

'

Spanish

'

,

'

Portuguese

'

)

Union

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 Lists The Python3 list