Answer» - The major difference between List and Tuple in PYTHON is that List is mutable whereas Tuple is immutable. Mutable means the structure or content can change after it is created whereas Immutable means the structure or content cannot be CHANGED after it is created.
- With the property of Immutability, Tuple is more memory efficient than List which means Tuple takes less memory than List because as List is mutable python needs to allocate more space as we can potentially add more entries to it.
- A tuple is also time-efficient because of the same property's Immutability. It takes less time for instantiating a new Tuple over List. Also, it takes less time to lookup in a Tuple than a List. Although the time difference isn't LARGE it is worthwhile to note.
- The list has higher flexibility than Tuple as we can add new elements delete existing elements etc easily but as Tuples are immutable adding a new element or deleting existing elements means that we NEED to create a new Tuple with the new addition or deletion.
import syslist_elements = [1,1,1,1,1,1]tuple_elements = (1,1,1,1,1,1)print('List memory took: ' + str(sys.getsizeof(list_elements)) + ' bytes')print('Tuple memory took: ' + str(sys.getsizeof(tuple_elements)) + ' bytes')# Output# List memory took: 104 bytes# Tuple memory took: 88 bytes
|