Try to answer the following question.
What are lists and tuples good for?
What is the complexity of a lookup in a list/tuple?
How is that complexity achieved?
What are the differences between lists and tuples?
How does appending to a list work?
When should I use lists and tuples?
Lists and tuples are a class of data structure called arrays. An array is simply a flat list of data with some intrinsic ordering.
This demarcates another line between lists and tuples: lists are dynamic arrays while tuples are static arrays.
Differences between lists and tuple
lists are dynamic arrays; they are mutable and allow for resizing(changing the number of elements that are held).
Tuples are static arrays; they are immutable, and the data within them cannot be changed once they have been created.
Tuples are cached by the Python runtime, which means that we don’t need to talk to the kernel to reserve memory every time we want to use one.
These differences outline the philosophical difference between the two: tuples are for describing multiple properties of one unchanging thing, and list can be used to store collections of data about completely disparate objects.
作者: Jason Leaster
本文采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可