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!
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
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!
I just checked memory usage by elements
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