Using list and tuple in Python

Source: Internet
Author: User

List

One of the data types built into Python is the list: lists. A list is an ordered set of elements that can be added and removed at any time.

For example, by listing the names of all the classmates in the class, you can use a list to indicate:

The variable classmates is a list. Use the Len () function to get the number of list elements:

Use the index to access the elements of each position in the list, and remember that the index starts at 0.

Python will report a indexerror error when the index is out of range, so make sure the index is not out of bounds, and remember that the index of the last element is Len (classmates)-1.

If you want to take the last element, in addition to calculating the index location, you can also use 1 to index, get the last element directly:

And so on, you can get the bottom 2nd, the bottom 3rd one:

Of course, the fourth to the bottom of the border.

The list is a mutable, ordered table, so you can append an element to the end of the list:

You can also insert elements into the specified position, such as the index number 1:

To delete the element at the end of the list, use the Pop () method:

To delete the element at the specified position, use the pop (i) method, where I is the index position:

To replace an element with another element, you can assign a value directly to the corresponding index position:

The data types of the elements in the list can also be different, such as:

The list element can also be another list, such as:

Note that s has only 4 elements, where s[2] is a list, which is easier to understand if it is disassembled:

To get ' php ' can write p[1] or s[2][1], so s can be regarded as a two-dimensional array, similar to three-dimensional, four-dimension ... arrays, but rarely used.

If a list does not have an element, it is an empty list with a length of 0:

Tuple

Another ordered list is called Ganso: tuple. Tuple and list are very similar, but once the tuple is initialized, it cannot be modified, for example, the name of the classmate is also listed:

Now, classmates this tuple cannot be changed, and it does not have a append (), insert () method. Other methods of acquiring elements are the same as lists, which can be used normally classmates[0],classmates[-1], but cannot be assigned to another element.

What is the meaning of immutable tuple? Because the tuple is immutable, the code is more secure. If possible, you can use a tuple instead of a list as much as possible.

The trap of a tuple: when you define a tuple, the elements of the tuple are determined when you define it.

If you want to define an empty tuple, you can write t= ()

But to define a tuple of only 1 elements, if you define this:

The definition is not a tuple, is 1 this number! This is because the parentheses () represent both the tuple, the parentheses of the mathematical formula, and the parentheses in the mathematical formula, which creates ambiguity, so Python rules that, in this case, the parentheses are calculated, and the result is naturally 1.

Therefore, only 1 elements of a tuple definition must be added with a comma to disambiguate:

When Python displays a tuple of only 1 elements, it also adds a comma to avoid misunderstanding the parentheses in the mathematical sense.

Finally, look at the variable tuple:

This tuple is defined by 3 elements, namely ' a ', ' B ', and a list.

The above changes are elements of list, not tuple elements. The list that the tuple initially points to is not changed to another list, so the so-called "invariant" of a tuple is that each element of a tuple is directed to never change. That point to ' a ', it cannot be changed to point to ' B ', pointing to a list cannot be changed to point to other objects, but the list itself is variable!

List and tuple are Python's built-in ordered set, one variable, one immutable. Choose to use them as needed.

Using list and tuple in Python

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.