Python list action (add delete) list and tuple differences

Source: Internet
Author: User
Tags python list

Actions for Python list (add delete)

1. Additions to the list: Append and Extend

The difference between append and extend: Append can only append 1 elements, extend may append multiple

>>> lst1=[1,2,3,4]>>> lst1.append (5)>>> lst1[1, 2, 3, 4, 5] >>> lst1.extend ([6,7,8])>>> lst1[1, 2, 3, 4, 5, 6, 7, 8]

2. Deletion and substitution of lists

Delete Using Del and pop

Use subscript to replace

>>> lst2=['1','Google','2','IBM','3','Huawei','4','XJZX']>>> lst2[1]='Baidu'>>>lst2['1','Baidu','2','IBM','3','Huawei','4','XJZX']>>> Lst2.pop (1)'Baidu'>>>lst2['1','2','IBM','3','Huawei','4','XJZX']>>>delLst2[0]>>>lst2['2','IBM','3','Huawei','4','XJZX']

3. use the number of elements in the "Len (name)" Statistics list

>>> Len (LST2)

6

Two.

The difference between a list and a tuple

1. The same point:

Tuples and lists store the container for the elements, and the subscripts are all starting at 0, separated by commas

2 different points

The list can be modified to change the data type, when defined as []

Tuples cannot be modified to be defined as ()

Python list action (add delete) list and tuple differences

Related Article

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.