The difference between a python list, a tuple, a dictionary

Source: Internet
Author: User
Tags python list

Tag: Indicates a disadvantage associated with a slice element in the case of an Operation collection

First, List

1. An ordered set of arbitrary objects
A list is a set of values of any type, combined in a certain order.
2. Read by offset
The values that make up the list are called elements (Elements). Each element is identified by an index, the first index is 0, and the function of the sequence can be implemented
3. Variable length, heterogeneous and arbitrary nesting
The elements in the list can be any type, even a list type, which means that the list can be nested
4. Variable sequence
Supports indexing, slicing, merging, deleting, and so on, and they are all in place to modify the list
5. Object reference Array
A list can be a normal array, and whenever a reference is used, Python always points the reference to an object, so the program only needs to manipulate the object. When assigning an object to a data structure element or variable name, Python always stores a reference to the object, not a copy of the object.

< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" > two, tuples

1. Ordered collection of arbitrary objects &NBSP;
Same as List &NBSP;
2. Via offset access  
Same as List &NBSP;
3. Non-mutable sequence type &NBSP;
is similar to a string, but the tuple is immutable, does not support any in-place modification of the list, does not support any method calls 4. Fixed length, heterogeneous, arbitrary nesting &NBSP;
fixed-length tuples immutable Fixed length without being copied, other same list &NBSP;
5. An array of object references &NBSP;
similar to list, Ganso is an array of object references

compared with list
1. Faster than list operation
2. "Write protection" of the data
3. Can be used in string formatting
4. Key that can be used as a dictionary

Third, the dictionary

1. Read by key instead of offset &NBSP;
Dictionary is an associative array. is a collection of objects indexed by a keyword, stored using key-value (Key-value), &NBSP;
2. An unordered collection of arbitrary objects &NBSP; the entries in the dictionary do not have a specific order, with the "key" as the symbol &NBSP;
3. Variable length, heterogeneous, arbitrary nesting The same list, nested can contain lists and other dictionaries such as &NBSP;
4. Belongs to the variable mapping type &NBSP;
because it is unordered, it cannot be sequenced, but can be modified at a distance and mapped to a value by a key. The dictionary is the only built-in mapping type (the object that the key maps to the value) &NBSP;
5. Object reference Table &NBSP; the
Dictionary stores object references, not copies, as well as lists. The key of the dictionary cannot be changed, list cannot be a key, string, Ganso, integer, etc. can be

compared with list, Dict has the following features:
1. The search and insertion speed is very fast and will not increase with the increase of key
2. Need to occupy a lot of memory, memory waste more
and the list is the opposite:
1. The time to find and insert increases as the element increases
2. Small footprint, little wasted memory
So, Dict is a way of exchanging space for time.

Iv. Collection

1. Is a set of keys, but does not store value, and key cannot be duplicated
To create a set, you need to provide a list as the input set, S = Set ([three-to-three]), note that the passed-in parameter [1, 2, 3] is a list, and the displayed set ([1, 2, 3]) just tells you that this set has the 3 elements of the inside of a "." This is a list
2. Duplicate elements are automatically filtered in set
Set can be regarded as a set of unordered and non-repeating elements in mathematical sense, so two sets can do the intersection and set of mathematical meanings.

There is also a collection of Forzenset (), which is a frozen collection, which is immutable and has a hash value, and the advantage is that it can be either a key to the dictionary or an element of another collection. The disadvantage is that once created it cannot be changed without the Add,remove method

Compare with Dict
The only difference between 1.set and dict is that there is no storage for the corresponding value
The principle of 2.set, like Dict, cannot be put into mutable objects, because there is no way to tell if two mutable objects are equal, there is no guarantee that the set will "have no duplicate elements"

The difference between a python list, a tuple, a dictionary

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.