I have a list of strings. I have a function that given a string returns 0 or 1. How can I delete all strings in the list for which the function returns 0?
Java List remove() method is used to remove elements from the list.
The method pop() can be used to remove and return the last value from the list or the given index value. If the index is not given, then the last element is popped out and removed.
In Python, use list methods clear() , pop() , and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.
To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument to the method.
[x for x in lst if fn(x) != 0]
This is a "list comprehension", one of Python's nicest pieces of syntactical sugar that often takes lines of code in other languages and additional variable declarations, etc.
See: http://docs.python.org/tutorial/datastructures.html#list-comprehensions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With