Junior — Middle
Which of these data types takes up less memory — list or tuple?
sobes.tech AI
Answer from AI
Tuples occupy less memory than lists in Python. This is because tuples are immutable, and their structure is more compact since they do not require additional space to support modification operations (such as adding or removing elements).
Example of comparison:
import sys
lst = [1, 2, 3, 4]
tpl = (1, 2, 3, 4)
print(sys.getsizeof(lst)) # for example, 88 bytes
print(sys.getsizeof(tpl)) # for example, 72 bytes
Thus, if data does not require modifications, using tuples helps save memory.