Skip to main content

Command Palette

Search for a command to run...

Tuple Vs Lists Size Difference

Published
2 min readView as Markdown

Hello Everyone,

Good Morning.

I have been Recently working on Memory efficiency of different data structures used in Python Language so I am presenting my thoughts on same with mostly widely used sequences Lists and Tuples.

This has been asked to me in lot of Interviews I am just adding one of base reason why would you prefer Tuple over lists.

The main reason is Tuple occupies less memory size as compared to List.

Python allocates memory to tuples in terms of larger blocks with a low overhead because they are immutable. On the other hand, for lists, Pythons allocates small memory blocks. At the end of it, the tuple will have a smaller memory compared to the list. This makes tuples a bit faster than lists when you have a large number of elements.

For example:
tuple_names = ('Nicholas', 'Michelle', 'Alex')
list_names = ['Nicholas', 'Michelle', 'Alex']
print(tuple_names.__sizeof__())
print(list_names.__sizeof__())
Output:
48
64

Let me know your thoughts on same and what other reasons you prefer tuple over list, let me know in comment section below.

Thanks. Happy Sunday

PS: Tuple is Immutable means you cannot change it's value post creation other than creating new object in memory.

#datascience #dataengineering #list #tuple #pythonprogrammer #python3

A

This is a good insight. By habit, I always choose Lists but I guess, based on the dataset, I'll be going for tuples more frequently. :)

V

Thanks for your response Atharva.Choose your sequence as per Problem.