Python3 List extend() Method
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
- seq -- a list of elements; it can be a list, tuple, set, or dictionary. If it is a dictionary, only the keys are appended to the end of the original list.
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
=
[
'
'
,
'
Runoob
'
,
'
Taobao
'
]
list2
=
list
(
range
(
5
)
)
Create a list of 0-4
list1
.
extend
(
list2
)
Extend the list
(
"
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
)
(
'
New list:
'
,
language
)
Add set elements to the end of the list
language
.
extend
(
language_set
)
(
'
New list:
'
,
language
)
新列表: ['French', 'English', 'German', 'Spanish', 'Portuguese']
新列表: ['French', 'English', 'German', 'Spanish', 'Portuguese', 'Chinese', 'Japanese']
The Python3 list