Home > Backend Development > Python Tutorial > python list中append()与extend()用法分享

python list中append()与extend()用法分享

WBOY
Release: 2016-06-16 08:46:36
Original
1276 people have browsed it

1. 列表可包含任何数据类型的元素,单个列表中的元素无须全为同一类型。
2.  append() 方法向列表的尾部添加一个新的元素。只接受一个参数。
3.  extend()方法只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中。

append()用法示例:

>>> mylist = [1,2,0,'abc']

>>> mylist

[1, 2, 0, 'abc']

>>> mylist.append(4)

>>> mylist

[1, 2, 0, 'abc', 4]

>>> mylist.append('haha')

>>> mylist

[1, 2, 0, 'abc', 4, 'haha']

>>>

extend()用法示例:

>>> mylist

[1, 2, 0, 'abc', 4, 'haha']

>>> mylist.extend(['lulu'])

>>> mylist

[1, 2, 0, 'abc', 4, 'haha', 'lulu']

>>> mylist.extend([aaa,'lalalala'])

Traceback (most recent call last):

  File "", line 1, in

NameError: name 'aaa' is not defined

>>> mylist.extend(['123123','lalalala'])

>>> mylist

[1, 2, 0, 'abc', 4, 'haha', 'lulu', '123123', 'lalalala']

>>> mylist.extend([111111,222])

>>> mylist

[1, 2, 0, 'abc', 4, 'haha', 'lulu', '123123', 'lalalala', 111111, 222]

>>>

OVER!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template