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!