Python中append和extend的区别的理解

append

append向列表中添加一个对象

重点是一个对象,把要添加的打成一包加到原始列表中。就算是一个序列, 加到列表中也只占一个对象的位子

举例

music_media = ['compact disc', '8-track tape', 'long playing record']
new_media = ['DVD Audio disc', 'Super Audio CD']
music_media.append(new_media)
print music_media
>>>['compact disc', '8-track tape', 'long playing record', ['DVD Audio disc', 'Super Audio CD']]

 

extend

extend向列表中添加一个序列,可以理解成两个序列的合并,这个序列里有几个对象就占几个位子

举例

music_media = ['compact disc', '8-track tape', 'long playing record']
new_media = ['DVD Audio disc', 'Super Audio CD']
music_media.extend(new_media)
print music_media
>>>['compact disc', '8-track tape', 'long playing record', 'DVD Audio disc', 'Super Audio CD']

 

代码参考:

https://www.cnblogs.com/subic/p/6553187.html

你可能感兴趣的:(python)