Basic types
βΉοΈ
NoneType
"Empty" value
No
None
bool
Boolean value (True
or False
)
No
True
int
Integer numbers
No
42
float
Floating point number
No
23.43
str
Textual data - sequense of characters
No
"Hello!"
list
Mutable sequense of any kind of objects
Yes
[1, 2, 3]
tuple
Immutable sequense of objects
No
(1, 2, 3)
set
Mutable collection of unique objects
Yes
{1, 2, 3}
frozenset
Immutable collection of unique objects
No
frozenset({1, 2, 3})
dict
The collection of key-value pairs
Yes
{"name": "Johnny", "second_name": "Walker"}
Main categories:
Mutable or Immutable
Is object allowing it's change after creation?
Hashable
Can this object be used as the key for the dictionary (has defined
__hash__()
and__eq__()
methods)?
Container, sequence, iterable
Has this object contained elements like other objects (has methods like
__iter__
or__getitem__()
)?
Callable
Can be invoked/run like a function or class (has method
__call__()
defined)?
Additional types:
range
function
code object
module
iterator
generator
slice object
And much more (because everything in Python is a object and thus - everything is of some specific type).
Sizes of the object of basic types:
π₯
import sys, rich
table = rich.table.Table(rich.table.Column(header="Object", style="blue"),
"Type",
rich.table.Column(header="Size", style="cyan", justify="right"),
title="Basic types memory usage", show_lines=True)
objects = [ None, True, 42, 3.1415, "Hello", [1, 2, 3], ("a", "b", "c"),
{1, 2, 3}, frozenset({1, 2, 3}), {"x": 1, "y": 2} ]
for obj in objects:
table.add_row(*map(str, (obj, type(obj), sys.getsizeof(obj))))
rich.console.Console().print(table)
Basic types memory usage
ββββββββββββββββββββββββ³ββββββββββββββββββββββ³βββββββ
β Object β Type β Size β
β‘ββββββββββββββββββββββββββββββββββββββββββββββββββββ©
β None β <class 'NoneType'> β 16 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β True β <class 'bool'> β 28 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β 42 β <class 'int'> β 28 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β 3.1415 β <class 'float'> β 24 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β Hello β <class 'str'> β 54 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β [1, 2, 3] β <class 'list'> β 80 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β ('a', 'b', 'c') β <class 'tuple'> β 64 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β {1, 2, 3} β <class 'set'> β 216 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β frozenset({1, 2, 3}) β <class 'frozenset'> β 216 β
ββββββββββββββββββββββββΌββββββββββββββββββββββΌβββββββ€
β {'x': 1, 'y': 2} β <class 'dict'> β 232 β
ββββββββββββββββββββββββ΄ββββββββββββββββββββββ΄βββββββ
π₯
Memory requirement to store elements of int
type in a different collections.
For 1 million of elements:
Tuple
8
List
9
Set
33
Dictionary
42
Memory size may not be the only criteria to select data type. Rather, time required to perform operation on data type can be critical criteria.
import sys, rich
n = 1000000
tests = {
"list": [*range(n)],
"tuple": tuple(range(n)),
"set": set(range(n)),
"dict": dict.fromkeys(range(n))
}
table = rich.table.Table("Type",
rich.table.Column(header="Total size", style="cyan", justify="right"),
rich.table.Column(header="Size per element", style="red", justify="right"),
title=f"Collections memory requirement ({n} elements)", show_lines=True
)
for type_, collection in tests.items():
# print(f"Size of <{type_}> ({n} items): {sys.getsizeof(collection)}, {sys.getsizeof(collection) / n} per element")
table.add_row(str(type_), str(sys.getsizeof(collection)), str(sys.getsizeof(collection) / n))
rich.console.Console().print(table)
Collections memory requirement (1000000
elements)
βββββββββ³βββββββββββββ³βββββββββββββββββββ
β Type β Total size β Size per element β
β‘ββββββββββββββββββββββββββββββββββββββββ©
β list β 8000056 β 8.000056 β
βββββββββΌβββββββββββββΌβββββββββββββββββββ€
β tuple β 8000040 β 8.00004 β
βββββββββΌβββββββββββββΌβββββββββββββββββββ€
β set β 33554648 β 33.554648 β
βββββββββΌβββββββββββββΌβββββββββββββββββββ€
β dict β 41943136 β 41.943136 β
βββββββββ΄βββββββββββββ΄βββββββββββββββββββ
Last updated
Was this helpful?