(python) Removing a dictionary element in a list

Kepler·2020년 5월 27일
0

How to delete a dictionary element in a list?
You might think, "well, that is a no-brainer, just use good-old del."

Well, not quite so. The del does remove an element, BUT only by index or slilce. Let's see an example.

However, if you don't know the exact index of the element that you want to delete, let's say, if there is an if statement inside your for loop, it doesn't work (Although, it does not throw an error.)

So, how do you remove a desired element from a list then?
use remove() method of list.

What remove does is that it removes an element by its value.

For more methods of list, check out this blog post https://note.nkmk.me/en/python-list-clear-pop-remove-del/.

profile
🔰

0개의 댓글