Python delステートメントでリストの末尾の要素を削除するサンプル

環境
Windows 11 Pro 64bit
Python 3.11

構文
del リスト変数名[-1]
delステートメントを使ってリストの末尾の要素を削除します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ns = [31, 42, 53, 64, 87,99]
del ns[-1]
print(ns)
ns = [31, 42, 53, 64, 87,99] del ns[-1] print(ns)
ns = [31, 42, 53, 64, 87,99]

del ns[-1]

print(ns)

実行結果
[31, 42, 53, 64, 87]

Python

Posted by arkgame