I started to watch MITOpenCourseWork in order to study some basics of computer science. Today, I got to look into the difference between list and tuple.
Lists and tuples are similar in ways that they are both
Let’s take a look using some code
test_list = ["apple", "banana", "orange", "kiwi", "watermelon"]
test_tuple = ("apple", "banana", "orange", "kiwi", "watermelon")
test_list[3] = "mango"
print(test_list)
["apple", "banana", "orange", "mango", "watermelon"]
test_tuple[3] = 7
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
You can see that the third index of the list has been changed to 7 but it failed to change an item of the tuple. This is because tuples are immutable.
I have also learned that Python allocates more memory to lists than tuples because the size of lists can be manipulated. Let’s go back to the tuple and the list.
import sys
print(sys.getsizeof(test_list)) # 120
print(sys.getsizeof(test_tuple)) # 80
You can also see that tuples are more memory-efficient than lists. and because of that, iterations over tuples are a lot faster than over lists if they are of the same size.
python -mtimeit '["apple", "banana", "orange", "kiwi", "watermelon"]'
>> 10000000 loops, best of 3: 0.096 usec per loop
python -mtimeit '("apple", "banana", "orange", "kiwi", "watermelon")'
>> 100000000 loops, best of 3: 0.0115 usec per loop
It may not be the best comparison, but you can see that iteration over the tuple is approximately 8 times faster.