Python list and pythonlist

Source: Internet
Author: User

Python list and pythonlist

Operation

1. Add

Append: adds an element to the end of the list.

Insert (n, element value): Add an element to the specified index

2. Delete

Pop (n): WHEN n is specified, the element at n is deleted. If n is not specified, the last element is deleted.

Del list [n]: delete a given element directly.

List. remove (obj)
Removes the first matching item of a value from the list.

3. Modify and query

You can directly access and modify a single element through subscript

A [-1] indicates the last element of the list, that is, the last element of the list.

A = [1, 2, 3, 4, 5]

A [:], a [:] is equivalent to a [0: n: 1]: used to access the elements from 0 to n-1 in the auto-increment mode, the number of elements is n, which is actually to access the entire list itself.

This has many functions, such

Only an odd number of items in the list are obtained using a [: 2].

An odd number element a starting from the third element [2: 2]

The element of the even number is a [1: 2].

The reverse order of the list is a [:-1], which is equivalent to a [-1:-1]. It refers to access from the last element of the list in reverse order.

 

Note:

1) [:] And =

Both of them can be assigned values, but = is to reference the assigned value. The obtained list points to the original content, modifying one list and the other changes.

[:] Is a value assignment. modifying a new or original list does not affect another list.

 

4. Others

List. count (obj)
Count the number of times an element appears in the list

List. index (obj)
Locate the index location of the first matching item of a value from the list

List. reverse ()
Elements in the reverse list

+: Directly combine the two lists into one.

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.