I have known for a while that the primary difference between lists and tuples in Python is that lists are mutable and tuples are not. Beyond that and the different methods available to them, I know very little about lists and tuples. Is there any other difference between them? Are there any advantages/disadvantages (aside from immutability) in using a tuple over a list in Python 3? Does one have a faster access time, or have a smaller memory size, or contain more methods, than the other? Are their internal structures different in any way? Or is a tuple just an immutable list, nothing more?
Both lists and tuples are internally implemented as arrays of references to the element objects. This way, both can be indexed and both require the same amount of memory for each element. Internally, they are both homogeneous (untyped references). Logically, they are both heterogeneous (automatic dereferencing, the type is bound to the target object).
The list can be modified, so the internal array is a dynamic array. The tuple cannot be modified, so it is internally just fixed-size array. From that point of view, tuples are simpler.
For which is faster or not, you can measure the concrete situation using the timeit module.
You should be aware of the fact that tuples are immutable only with respect to the number and to values of the stored references. If (say) a list is used as one of the tuple elements, the list content can be changed. This way, logically, the tuple content is not constant (such tuple is not hashable).
Use whatever type is better for the purpose. There is no strict preference. It depends on the situation.
Run dir on both of them - pretty different method list (pop demonstrated below). tuples may be faster
>>> alist = [1,2,3]
>>> atuple = (1,2,3)
>>> alist.pop()
3
>>> atuple.pop()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'pop'
'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort' are all available for lists, and not for tuples - which makes sense, given the immutability idea.
Philosophically, some people expect lists to be homogeneous, and don't have that expectation of tuples.
In Python, there are two types of data structures: list and tuple. The list has dynamic properties, while the tuple has static ones. In other languages, lists are declared similarly to arrays. Lists don't have to be homogeneous all of the time, which makes it Python's most effective tool. The list is a type of container in Python's Data Structures that is used to store several pieces of data at once. Lists are a good way to keep track of a sequence of data and iterate over it.
Tuple is a sequence data form that can contain elements of various data types, but it is immutable. In other words, a tuple is a comma-separated list of Python properties. Because of its static existence, the tuple is quicker than the array.
Here are some differences I managed to get;
- Lists are mutable
- Tuples are immutable
Lists - Implication of iterations is Time-consuming. Tuple - The implication of iterations is comparatively Faster.
Lists -The list is better for performing operations, such as insertion and deletion. Tuple -Tuple data type is appropriate for accessing the elements.
Lists -Lists consume more memory. Tuple -Tuple consume less memory as compared to the list.
Lists -Lists have several built-in methods. Tuple -Tuple does not have many built-in methods.
Lists -The unexpected changes and errors are more likely to occur. Tuple -In tuple, it is hard to take place.
Hope you understand.
© 2022 - 2024 — McMap. All rights reserved.