Home >Backend Development >Python Tutorial >What is append in python?

What is append in python?

silencement
silencementOriginal
2019-06-24 13:50:2911519browse

What is append in python?

The append() function in python adds a new object at the end of the list, and the added object is the whole. Corresponding to append is the extend function.

There are many explanations on the difference between these two functions on the Internet, but I feel that they are not very clear and the memory is not deep. This explains clearly and is easy to remember.

list.append(object) Add an object object to the list
list.extend(sequence) Add the contents of a sequence seq to the list

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']]

As above, When using append, new_media is regarded as an object, and the entire package is added to the music_media object.

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']

As above, when using extend, new_media is regarded as a sequence, this sequence is merged with the music_media sequence, and placed behind it.

The above is the detailed content of What is append in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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