Python basic notes-sequence of data types (3)

Source: Internet
Author: User

List VS tuples

One question that is often asked is, "Why should we differentiate between tuples and list variables?" The question can also be expressed as "do we really need two similar sequence types?" , one reason is that in some cases, using one of these types is preferable to using a different type. One of the best ways to use immutable type variables is if you are maintaining some sensitive data, and you need to pass that data on to a function that you do not understand (perhaps an API that is not written at all), as an engineer who is only responsible for a certain part of the software, if you are sure that your data will not be tampered with by the calling function , you will feel a lot safer.

An example of a parameter that requires a mutable type is if you are managing a Dynamic data collection. You need to create them first, add them gradually or irregularly, or sometimes remove individual elements. This is a typical example of a variable type object that must be used. Fortunately, with the built-in list () and tuple () conversion functions, you can easily convert between the two.

The list () and tuple () functions allow you to create a tuple with a list, and vice versa. If you have a tuple variable, but you need a list variable because you want to update its object, the list () function is your best helper. If you have a list variable, And you want to pass it to a function, maybe an API, and you don't want anyone messing up your data, then the tuple () function is very useful.

Python basic notes-sequence of data types (3)

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.