Python daily-list

Source: Internet
Author: User

What's the list:

The list feels like the most python-brought data structure in Python's everyday programming. But Python's heavy list is not the same as in other languages.

Juvenile.. I wonder if you've heard that the Append method in Python is much more efficient than the Insert method? What, you don't know? Please allow Swaiiow to come slowly.

The list in other programming languages is also called a linked list, which is probably except for the last element, all elements have a pointer, this pointer only want the next element node reference.

If you want to add an element, point to the pointer in the last element that you want the app element to be inserted into? Hey, you can just point to the element you want to insert before you insert the element, and then point to the element you want to insert.

Sounds good, doesn't it?
In fact, it is also good! (This refers to a one-way list, of course)

And Python is doing this, he's storing the list structure is a whole block of memory, imagine you bought a bucket of badminton ball and then you want to take out the penultimate, think is xxxxxx

Specific to the insert operation above, in principle, each insert operation to move all the elements on the right side, and sometimes even re-establish a new memory space. To avoid this, the concept of dynamic arrays is usually used, and the core is to allocate a larger amount of memory when it is first created

It sounds like a rip-off, and think about it so that append and insert operations are very similar, but actually this kind of behavior can really reduce the cost.

My suggestion for list is that if you have a regular operation, or even a large number of requirements, then let the list bar.

Python daily-list

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.