Lists vs tuple in Python

Question:

I create my first python student project. I need save data about orders in online shop in books python. But I can’t understandwhat will be better: save informations in list with tuple (last mean in data must be changeable) or I can make just list. I see, that list with tuple take memorys less then list and tuple(!!!). But I was shoked, when I saw that all three methods – tuples, list and list with tuple, take memorys identically.
Quastion: 1. why list with tuple take memorys less, then just tuple? 2. why with all three methods dictionaries weigh the same? Thank you!enter image description hereenter image description here

I just checked memory usage by elements

Answers:

The issue is that "getsizeof" is not "recursive" in any meaningful way. When you create a list of tuple and a string, it is actually a list with a reference and a string. So the actual size of this first argument does not matter

([1,2],3).__sizeof__() == ([1,2,3,4,5,6,7],3).__sizeof__()

Same with dictionaries, you are getting size of the structure of key->reference, not key->dereferenced object

Answered By: lejlot
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.