Home  >  Article  >  Backend Development  >  What does append mean in python?

What does append mean in python?

silencement
silencementOriginal
2019-06-20 11:56:0764853browse

What does append mean in python?

There are many explanations on the difference between these two functions on the Internet, but I feel that they are not very clear and my 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, use append At this time, 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 does append mean 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