How do I remove an element from a list if it matches a substring?
I have tried removing an element from a list using the pop()
and enumerate
method but seems like I'm missing a few contiguous items that needs to be removed:
sents = ['@$\tthis sentences needs to be removed', 'this doesnt', '@$\tthis sentences also needs to be removed', '@$\tthis sentences must be removed', 'this shouldnt', '# this needs to be removed', 'this isnt', '# this must', 'this musnt'] for i, j in enumerate(sents): if j[0:3] == "@$\t": sents.pop(i) continue if j[0] == "#": sents.pop(i) for i in sents: print i
Output:
this doesnt @$ this sentences must be removed this shouldnt this isnt #this should this musnt
Desired output:
this doesnt this shouldnt this isnt this musnt
The remove() method removes the first matching element (which is passed as an argument) from the list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.
Use del to remove an element by index, pop() to remove it by index if you need the returned value, and remove() to delete an element by value. The last requires searching the list, and raises ValueError if no such value occurs in the list.
You can use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index. Therefore, a[2] contains 3 and pop() removes and returns the same as output. You can also use negative index values.
How about something simple like:
>>> [x for x in sents if not x.startswith('@$\t') and not x.startswith('#')] ['this doesnt', 'this shouldnt', 'this isnt', 'this musnt']
This should work:
[i for i in sents if not ('@$\t' in i or '#' in i)]
If you want only things that begin with those specified sentential use the str.startswith(stringOfInterest)
method
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