The difference between Python append, Extend, and insert _python

Source: Internet
Author: User
Tags extend

Recently, when I learned the Python language and saw adding more data to the list by append (), Extend (), the Insert () method was passed around.

Append and extend all need only one parameter and are automatically added to the end of the array, and if you need to add more than one, the array can be nested, but append is the nested array as an object,

Extend is to add nested array contents as multiple objects to the original array

As a basis for programming 0 small white, feel the need to comb it again:

The 1.append () method refers to adding a data item at the end of the list.

For example, add the "Gavin" item at the end of the students list.

>>> students = [' Cleese ', ' Palin ', ' Jones ', ' Idle ']
>>> students.append (' Gavin ')
>>& Gt Print (students)
[' Cleese ', ' Palin ', ' Jones ', ' Idle ', ' Gavin ']

The 2.extend () method refers to adding a data collection at the end of the list.

For example, on the basis of Example 1, the end of the students list continues to add "Kavin" and "Jack" and "Chapman" three items.

>>> students = [' Cleese ', ' Palin ', ' Jones ', ' Idle ']
>>> students.append (' Gavin ')
>>& Gt Print (students)
[' Cleese ', ' Palin ', ' Jones ', ' Idle ', ' Gavin ']
>>> students.extend ([' Kavin ', ' Jack ', ' Chapman ']
>>> print (students)
[' Cleese ', ' Palin ', ' Jones ', ' Idle ', ' Gavin ', ' Kavin ', ' Jack ', ' Chapman ']

The 3.insert () method refers to adding a data item before a specific location.

For example, add "Gilliam" before "Palin" in the students original list.

>>> students = [' Cleese ', ' Palin ', ' Jones ', ' Idle ']
>>> students.insert (1, ' Gilliam ')
> >> print (students)
[' Cleese ', ' Gilliam ', ' Palin ', ' Jones ', ' Idle '].

Because the data items are stacked from the bottom up, the first data number in the stack is 0, the second data number is 1, so it is Students.insert (1, ' Gillam ').

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.