Python built-in data structures

Source: Internet
Author: User
Tags set set

Built-in data structures

    • Variable
      1. List lists
      2. Byterray
      3. Dict Dictionary
      4. Set Set
    • Not variable
      1. Tuple tuples
      2. bytes
      3. Constants (Int,str,bool, etc.)

list (recommended index)
The list is equivalent to an orderly queue, which can be understood as an orderly contiguous space in memory, which is indexable , mutable, ordered, linear, and iterative .
Advantages:
Search by index (index), replace element efficiency high time complexity O (1).
Trailing append (append) and trailing (POP) element fast efficiency high Time complexity O (1).
Disadvantages:
Adding or removing elements from the middle makes the overall movement of the list elements inefficient.
Because it is a contiguous space of 1, if a large space is opened, memory space is not enough, it will trigger GC garbage collection.

tuple tuples (recommended use index)
Tuples can be understood in memory as a list, which is indexed, immutable, ordered, linear, and iterative .
Advantages:
The time complexity of finding elements by index is O (1).
Disadvantages:
Tuples are immutable, are read-only, and cannot be modified.

Set Set
Set in memory is scattered, discontinuous, non-indexed, he is going to heavy variable unordered can be iterated hash.
Advantages:
Queries whether elements are in the collection (using hash) time complexity O (1) by numeric values.
add element (add) time complexity O (1)
You can use set operations (and set | , Intersection &, symmetric difference ^, also available <, > subset superset).
Disadvantages:
You cannot use an index, you cannot place an element of the same content, and you cannot put a non-hash element (list,set,dity).

Dity Dictionary
Dity in memory are scattered, discontinuous, will reserve the hash space. The unordered variable iterative key is the de-heavy, hash.
Advantages:
Fast (using hash) time complexity O (1) through numeric queries.
Increase kv to TIME complexity O (1).
Remove the kv pair (pop) time complexity O (1).
Disadvantages:
You cannot use an index, you cannot place an element of the same content, and you cannot put a non-hash element (list,set,dity).
Memory consumption is larger relative to list.

Python built-in data structures

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.