Python | Difference Between List and Tuple
List and Tuple in Python are the classes of Python Data Structures. The list is dynamic, whereas the tuple has static characteristics. This means that lists can be modified whereas tuples cannot be modified, the tuple is faster than the list because of static in nature. Lists are denoted by the square brackets but tuples are denoted as parenthesis.
Important differences between List and Tuple in Python
|1||Lists are mutable||Tuples are immutable|
|2||The implication of iterations is Time-consuming||The implication of iterations is comparatively Faster|
|3||The list is better for performing operations, such as insertion and deletion.||Tuple data type is appropriate for accessing the elements|
|4||Lists consume more memory||Tuple consumes less memory as compared to the list|
|5||Lists have several built-in methods||Tuple does not have many built-in methods.|
|6||The unexpected changes and errors are more likely to occur||In tuple, it is hard to take place.|
List vs Tuple
Test whether tuples are immutable and list are mutable
Here we are going to compare the list and tuple mutability test.
Original list [1, 2, 4, 4, 3, 3, 3, 6, 5] Example to show mutability [1, 2, 4, 77, 3, 3, 3, 6, 5]
We can see here tuple can not be modified.
Traceback (most recent call last): File "e0eaddff843a8695575daec34506f126.py", line 3, in tuple1=4 TypeError: 'tuple' object does not support item assignment
Test whether tuples are memory efficient.
As tuples are stored in a single memory block therefore they don’t require extra space for new objects whereas the lists are allocated in two blocks, first the fixed one with all the Python object information and second a variable sized block for the data.
Test whether implication of iterations is comparatively faster in tuples
As tuples are stored in a single memory block therefore they don’t require extra space for new objects as they are immutable whereas the lists are allocated in two blocks, first the fixed one with all the Python object information and second a variable sized block for the data which makes them even more faster.
Total lookup time for Tuple: 7038208700 Total lookup time for LIST: 19646516700