Python getsizeof()関数でリストのデータサイズをバイト単位で取得する

環境
Python 3.9.13
Windows 10 Home 64bit
PyCharm 2022.2.1 (Community Edition)

構文
リスト変数名 =[要素1,要素2,…]
sys.getsizeof(リスト変数名)
getsizeof()関数を使ってリストのデータサイズを取得します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import sys
lst = [55, 66, 14, 46, 120, 210,333]
res = sys.getsizeof(lst)
print("リストのデータサイズを取得する結果")
print(res)
import sys lst = [55, 66, 14, 46, 120, 210,333] res = sys.getsizeof(lst) print("リストのデータサイズを取得する結果") print(res)
import sys
lst = [55, 66, 14, 46, 120, 210,333]

res = sys.getsizeof(lst)

print("リストのデータサイズを取得する結果")
print(res)

実行結果
120

Python

Posted by arkgame