-1

I have a list of names I have scraped from a website. In that list, there are some observations that are not names but are called "Home ". I can remove them by uni_name.pop(), but this is not easily repeatable.

How do I remove all values from the list that is "Home " at once?

Thank you in advance!

I tried writing this:

uni_name(filter(Home    ).__ne__, uni_name)

But it gave me an NameError: name 'Home' is not defined

AKX
  • 152,115
  • 15
  • 115
  • 172
  • `Home` is a variable that (as the error correctly states) is not defined. `"Home"` would be a string that you need (probably with some trailing spaces). – Yevhen Kuzmovych Feb 13 '23 at 10:19
  • @YevhenKuzmovych But even then, OP's use of `filter()` would be nonsensical. – AKX Feb 13 '23 at 10:20
  • can you provide an example of what the data looks like, and what you're trying to achieve? – arianhf Feb 13 '23 at 10:22
  • @AKX that is true. I'm just explaining the error. The approach in your answer is a much better way to do it anyway (though I'd argue that this question should be closed as duplicate) – Yevhen Kuzmovych Feb 13 '23 at 10:23
  • Does this answer your question? [Remove all occurrences of a value from a list?](https://stackoverflow.com/questions/1157106/remove-all-occurrences-of-a-value-from-a-list) – Yevhen Kuzmovych Feb 13 '23 at 10:23

1 Answers1

0

To remove all "Home"s (ignoring any number of leading/trailing spaces), the Pythonic way is to use a list comprehension.

>>> uni_name = ["hello", "Home          ", "   Home", "World"]
>>> uni_name = [name for name in uni_name if name.strip() != "Home"]
>>> print(uni_name)
['hello', 'World']
AKX
  • 152,115
  • 15
  • 115
  • 172