1. Python

Comparing Python Lists and Tuples

Well from a beginner perspective Lists and Tuples seems kind of similar expect some syntax difference and mutability?🤔 List have syntax as Square Brackets while Tuples have Circular Brackets. For example – [1, 2, 3] is a Python List while (1, 2, 3) is a Tuple.

This is the question which I asked my professor in Introduction to Python class and he kind of stumbled upon giving me some convincing answer. Then I tried to find answer on Google but did not find anything which clears compares Python List to Tuples one-on-one. So I did a lot of research and talked to some of my friends working as Research Scientists with Fortune 500 companies. After all this mind cringing, I found out some meaningful information, which I will be sharing here.
Let’s get straight into kind of comparing Lists with Tuples from a beginner perspective.

If your not aware of What are Python Lists or Tuples then please read Python Lists and Tuples in Python articles

Differences Between Features of Python Lists and Tuples

Lists are mutable meaning can be changed after creationTuples are immutable meaning these cannot be changed once created
Lists have updating operations/functionsTuples don’t have any updating operations/functions
Lists consume more memoryTuples consume less memeory comparitively to Lists
Owing to lists being mutable, chances of occurrence of errors increasesAs Tuples are fixed throughout program, this lowers chances of error occurring

Comparing Memory Consumed by Python List versus Tuple

There is a little difference in memory being consumed by a list containing same values as a Tuple. The fun part here is that you can verify this by your self using Python’s in built sys module function getsizeof(), which returns number of bytes an object is occupying in the memory.
Let’s try an example just open up Terminal/Command Line and type in Python3, this will start an interactive shell inside Terminal/Command Line.

>>> import sys
>>> l = [19, 28, 19]
>>> t = (19, 28, 19)
>>> sys.getsizeof(l)
>>> sys.getsizeof(t)

From above example, you can clearly see that there is a little difference between memory being occupied by list versus tuple both containing same values(Lists occupy 16 bytes more).
Well next question would be Why Lists need more memory space than tuple? As Lists can be updated later in program that’s why Python beforehand allocates some additional memory. So that Python does not need to reallocate some memory to list when list need to be updated.

Comparing Time of Accessing Values from List versus Tuple

Accessing values from a Tuple is little bit faster as compared to that from a List. For example – On MAC Terminal you can run following code to test this. Here I’m using timeit commandline tool.

$ python -m timeit -s "foo = [1, 2, 3]" "foo[0]"
20000000 loops, best of 5: 13.7 nsec per loop

$ python -m timeit -s "foo = (1, 2, 3)" "foo[0]"
20000000 loops, best of 5: 12.3 nsec per loop

So its clear from above example that accessing values from tuple is little bit faster as compared to list(Just 1.4 second in example only).

Which one(List or Tuple) to use when?

There are some advantages/disadvantages in using List over Tuple or vice-versa. But in terms of performance there is not any major difference(time to access values/memory space needed), but for dealing with very large number of values I would give preference to using Tuple over List.

Final Thoughts

It depends which one List or Tuple to use in your program. If in case you was trying to figure this out for your program, then please come in comments and post some sample code. I would try to help you out in making a choice out of List or Tuple.

More about Python Programming Language

Comments to: Comparing Python Lists and Tuples

Your email address will not be published.