Python リストには、リストから項目を削除するためのさまざまな組み込みメソッドがあります。これらとは別に、さまざまな方法を使用して、位置を指定してリストから要素を削除することもできます。この記事では、リストから項目を削除するためのさまざまな Python メソッドを検討します。
例
Input: ['Rose',' Lily', 'Lotus', 'Sun', 'Sunflower'] Delete: 'Sun' Output: ['Rose',' Lily', 'Lotus', 'Sunflower'] Explanation: In this, we have removed the 'Sun' element from the given list.>
リストから項目を削除する
別の方法を使用して、リストから要素を削除します。 パイソン :
- 使用する Python の削除()
- 使用する パイソン
- Pythonの使用 リストの内包表記
- 使用する Python ポップ()
- 使用する Python の破棄()
- 使用する Python フィルター()
- 使用する Python リストのスライス
1.remove()を使用してリストから要素を削除します。
削除する項目の値をパラメーターとして渡して () 関数を削除することで、リストから要素を削除できます。
Python3
lst>=> [>'Iris'>,>'Orchids'>,>'Rose'>,>'Lavender'>,> >'Lily'>,>'Carnations'>]> print>(>'Original List is :'>, lst)> # using remove()> lst.remove(>'Orchids'>)> print>(>'After deleting the item :'>, lst)> |
>
>出力
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations'] After deleting the item : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']>
2. del() を使用してリストから要素を削除します。
Del() を使用してリストから要素を削除できます。の パイソン ステートメントは List の関数ではありません。 del文で削除したい項目(要素)のインデックスを指定してリストの項目を削除できます。
Python3
リンクリストと配列リスト
lst>=> [>'Iris'>,>'Orchids'>,>'Rose'>,>'Lavender'>,> >'Lily'>,>'Carnations'>]> print>(>'Original List is :'>, lst)> # using del statement> # to delete item (Orchids at index 1)> # from the list> del> lst[>1>]> print>(>'After deleting the item :'>, lst)> |
>
>出力
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations'] After deleting the item : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']>
3. リスト内包表記を使用してリストから要素を削除する
反復中にリストから要素を削除できます。この方法では、 リストの内包表記 。ここでは、削除する必要がある要素を除くすべての要素を追加しています。
Python3
# Python program to remove given element from the list> list1>=> [>1>,>9>,>8>,>4>,>9>,>2>,>9>]> > # Printing initial list> print> (>'original list : '>+> str>(list1))> # using List Comprehension> # to remove list element 9> list1>=> [ele>for> ele>in> list1>if> ele !>=> 9>]> > # Printing list after removal> print> (>'List after element removal is : '> +> str>(list1))> |
>
>出力
original list : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [1, 8, 4, 2]>
4. Pop() を使用してリストから要素を削除する
Pop() を使用してリストから要素を削除できます。 Pop() もリストのメソッドです。指定したインデックスにある要素を削除し、次を使用してその要素の値を取得できます。 ポップ() 。
Python3
Java文字列の部分文字列
lst>=> [>'Iris'>,>'Orchids'>,>'Rose'>,>'Lavender'>,> >'Lily'>,>'Carnations'>]> print>(>'Original List is :'>, lst)> # using pop() to delete item> # ('Orchids' at index 1) from the list> a>=> lst.pop(>1>)> print>(>'Item popped :'>, a)> print>(>'After deleting the item :'>, lst)> |
>
>出力
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations'] Item popped : Orchids After deleting the item : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']>
5.discard()を使用してリストから要素を削除する
Discard() を使用してリストから要素を削除できます。このメソッドでは、リストをセットに変換し、discard() 関数を使用して項目を削除します。次に、セットをリストに変換して戻します。
Python3
# Python program to remove given element from the list> lst>=> [>'Iris'>,>'Orchids'>,>'Rose'>,>'Lavender'>,> >'Lily'>,>'Carnations'>]> print>(>'Original List is :'>, lst)> # using discard() method to remove list element 'orchids'> lst>=> set>(lst)> lst.discard(>'Orchids'>)> # Converting set back to list> lst>=>list>(lst)> print>(>'List after element removal is :'>, lst)> |
>
>
出力:
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations'] List after element removal is : ['Lily', 'Carnations', 'Iris', 'Rose', 'Lavender']>
注記: リストはセットに変換されるため、重複するものはすべて削除され、リストの順序は保持されません。
6. filter() を使用してリストから要素を削除する
filter() を使用してリストから要素を削除できます。このメソッドでは、filter() 関数を使用してリストから不要な要素をフィルターします。
Python3
# Python program to remove given element from the list> lst>=> [>'Iris'>,>'Orchids'>,>'Rose'>,>'Lavender'>,> >'Lily'>,>'Carnations'>]> print>(>'Original List is :'>, lst)> # using discard() method to remove list element 'orchids'> lst1>=> filter>(>lambda> item: item!>=>'Orchids'>,lst)> print>(>'List after element removal is :'>,>list>(lst1))> |
>
>
出力
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations'] List after element removal is : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']>
7. スライスを使用してリストから要素を削除する
スライスを使用してリストから要素を削除できます。このメソッドは、元のリストをスライスし、削除された要素を含まない部分を連結することにより、新しいリストを作成します。
Python3
my_list>=> [>1>,>2>,>3>,>4>,>5>]> my_list>=> my_list[:>2>]>+> my_list[>3>:]> print>(my_list)># Output: [1, 2, 4, 5]> |
>
>
出力:
Ubuntu の ipconfig
[1, 2, 4, 5]>
8. Itertoolsを使用してリストから要素を削除する
itertools を使用してリストから要素を削除できます。コードでは、 itertools.filterfalse() 指定されたリストから数字 9 が出現するすべてを削除する関数。
要素が 9 に等しいかどうかを確認するラムダ関数を作成し、フィルターをリストに適用します。結果として得られるフィルター処理されたリストが出力として印刷されます。
Python3
import> itertools> lst>=> [>1>,>9>,>8>,>4>,>9>,>2>,>9>]> print>(>'Original List is :'>, lst)> # itertools.filterfalse() to filter out all occurrences of 9 from the list> lst_filtered>=> list>(itertools.filterfalse(>lambda> x: x>=>=> 9>, lst))> print>(>'List after element removal is :'>, lst_filtered)> #this code is contributed by Jyothi pinjala.> |
>
>
出力
Original List is : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [1, 8, 4, 2]>
この記事では、リストから項目を削除するさまざまな方法について説明しました。この記事では合計 8 つの方法について説明します。リストからの要素の削除は組み込み関数を使用して実行できますが、従来とは異なる方法も使用しました。
類似の記事:
- 指定された要素をリストから削除します
- 特定のリスト要素を削除する方法
- リストの最初の要素を削除する
- Python でリストから複数の要素を削除する