The tuple tuples in Python are detailed

Source: Internet
Author: User
Tuple is immutable list. Once a tuple has been created, it cannot be changed in any way.

The same as the Tuple and list

A tuple is defined in the same way as a list, except that the entire set of elements is surrounded by parentheses rather than square brackets.
The elements of a Tuple are sorted in the order defined by the list. The tuples index is the same as list starting from 0, so the first element of a non-empty tuple is always t[0].
A negative index is counted as a list from the tail of a tuple.
As with list shards (slice) can also be used. Note that when a list is split, a new list is obtained, and when a tuple is split, a new tuple is obtained.

A method that does not exist for a Tuple

You cannot add elements to a tuple. The Tuple does not have a append or extend method.
You cannot delete an element from a tuple. The Tuple does not have a remove or pop method.
You cannot find elements in a tuple. Tuple does not have an index method.
However, you can use in to see if an element exists in a tuple.

Benefits of using Tuple

A Tuple operates faster than a list. If you define a constant set of values and the only thing you want to do with it is to iterate over it, use tuple instead of list.
You can make your code more secure if you write-protect the data that you do not need to modify. Using a tuple instead of a list is like having an implied Assert statement, which indicates that the data is a constant. If you have to change these values, you need to perform a tuple-to-list conversion.

Conversion of Tuple to list

A Tuple can be converted into a list and vice versa. The built-in tuple function receives a list and returns a tuple with the same element. The list function receives a tuple and returns a list. In effect, a tuple freezes a list, and the list thaws a tuple.

  • 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.