2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[python] リストの要素の削除

Posted at

メモ:list().removeとdelの違い

リストの関数であるremoveは先頭から見てはじめに一致した要素の削除。
delは、引数で受けっとったオブジェクトの削除
リストの関数popを使うと、delと同じようなことができますが、こちらは要素のインデックスを指定します。

test.
>>>a = [1,2,3,4,5,1]
>>>a.remove(a[5])
>>>a
[2,3,4,5,1]
>>>a = [1,2,3,4,5,1]
>>>del(a[5])
>>>a
[1,2,3,4,5]
>>>a = [1,2,3,4,5,1]
>>>a.pop(5)
1
>>>a
[1,2,3,4,5]

#まとめ
基本的にdelかpopを使ったほうが間違いが起きにくい

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?