Python スライシングを使ってリストの末尾の要素を削除する

環境
Windows 11 Pro 64bit
Python 3.11

構文
リスト名 = リスト名[:-1]
スライシングを使って、リストの末尾の要素を削除します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
res = [21, 32, 43, 54, 60]
k = res[:-1]
print(k)
res = [21, 32, 43, 54, 60] k = res[:-1] print(k)
res = [21, 32, 43, 54, 60]

k = res[:-1]

print(k)

実行結果
[21, 32, 43, 54]

Python

Posted by arkgame