Pythonは人気プログラミング言語の1つです。
そんなPythonをやる上で必ず出てくるのがリストです。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
リストを使いこなすことで、スキルアップ間違いなしです!
リストに関するまとめ記事を作成しました。他の記事もここからどうぞ!
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
今回はリストの要素の消去の仕方を紹介します!
画像に使用しているのは、グーグルコラボラトリーでのコードです。
グーグルコラボラトリーは環境構築が不要でPythonを使えます!
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
特に初心者さんにはおすすめです!
↓詳しくはこちらの記事で紹介しています。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/Slide1-3-1-300x169.jpg)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/Slide1-3-1-300x169.jpg)
リストの要素の消去する3つの方法
今回は次のリストを消去する3つの方法を紹介・解説します。
- clear : リストの要素を全消去
- del : 要素の位置・範囲を指定して消去
- remove : 指定した要素で検索し、最初の要素を消去
同じ要素を消去する役割ですが、ちゃんと使い方が違います。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
それぞれの特徴を押さえて使いこなそう!
clear : リストの要素を全消去
clearを使うとリストの要素を一度に全て消去できます。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
clearはその名の通り、リストを「スッキリ」してくれます
clearは次のように使います。
![](https://yosshilifeup.com/wp-content/uploads/2022/02/a54bd5ce77f86643a1964b01a859f3c4.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/a54bd5ce77f86643a1964b01a859f3c4.png)
リストを全部消去するので、引数は取りません。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
簡単に要素を消去できます!
要素を『全部』消去する
clearの注意点をあえていうなら、要素を全部消去するということです。
特定の要素を残したい、一部の要素を消去したいときは他の方法で消去しましょう!
del : 要素の位置・範囲を指定して消去
del文を使うと位置・範囲を指定して特定の要素を消去することができます。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
delはおそらく英語の「delete」からきていて、「〜を消去する」という意味です
del文は次のように使います。
![](https://yosshilifeup.com/wp-content/uploads/2022/02/55623672046ac3324ea6ada8ff053ec3.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/55623672046ac3324ea6ada8ff053ec3.png)
このように引数に消去したい要素のインデックスを渡すと、その要素だけを消去できます。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
スライスを使うと一度に複数消去できます
![](https://yosshilifeup.com/wp-content/uploads/2022/02/5c4d202b622ac602b73aeababd781fc1.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/5c4d202b622ac602b73aeababd781fc1.png)
引数にどこからどこまでをインデックスで指定すると、選択された範囲の要素が消去されます。
範囲の指定の仕方
ここで、気をつけることは「範囲の指定の仕方」です
![](https://yosshilifeup.com/wp-content/uploads/2022/02/5c4d202b622ac602b73aeababd781fc1-1.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/5c4d202b622ac602b73aeababd781fc1-1.png)
先ほどの画像ですが、範囲を「1」から「3」までと指定しています。
しかし、消去した後のリストを見ると「apple」と「peach」が残っています。
これは元のリストのインデックスの「0」と「3」に当たります。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
1から3までの範囲を選んだのに3が残っている!
これはdel文に限らず、for文の範囲選択などpythonでは共通です。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
意外と間違いやすいところなので気をつきよう!
remove : 指定した要素で検索し、最初の要素を消去
removeを使うと要素を指定して消去することができます。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
removeは英語で「取り除く」という意味です
removeは次のように使います。
![](https://yosshilifeup.com/wp-content/uploads/2022/02/fc602207380e0663acb693ac43113ba4.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/fc602207380e0663acb693ac43113ba4.png)
del文と違って、removeは要素を指定して消去することができます。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
ここで、注意する点が2つあります
- 複数要素がある場合、最初の要素だけ消去される
- 要素を指定するので、要素の型をしっかり覚えておく
複数要素がある場合、最初の要素だけ消去される
消したい要素がリストの中に複数含まれるときは、最初の要素だけが消去されます。
![](https://yosshilifeup.com/wp-content/uploads/2022/02/fc602207380e0663acb693ac43113ba4-1.png)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/fc602207380e0663acb693ac43113ba4-1.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
2個目の’apple’だけを消去したいときは、del文でインデックスを指定して消去しよう!
要素を指定するので、要素の型をしっかり覚えておく
今回のように文字列のリストのときは特に注意が必要です。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
落ち着いて1つずつ確実に!
まとめ
![](https://yosshilifeup.com/wp-content/uploads/2022/02/photo-1559925393-8be0ec4767c8.webp)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/photo-1559925393-8be0ec4767c8.webp)
今回はリストの要素の消去ということで、clear・del・removeを紹介しました。
やはり、要素を消去するだけでもやり方は様々です。
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
それぞれの使いかたを覚えて、適材適所で使いこなそう!
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
![](https://yosshilifeup.com/wp-content/uploads/2021/12/125cba9f0f9741e255115b26fda0b117.png)
また、別の記事で!
![](https://yosshilifeup.com/wp-content/uploads/2022/02/e8f26a2128febdb8d223c1b43e2a7d59-300x169.jpg)
![](https://yosshilifeup.com/wp-content/uploads/2022/02/e8f26a2128febdb8d223c1b43e2a7d59-300x169.jpg)
コメント