Remove the first occurrence that matches the criteria from the list.

Suppose I have a list of strings:

first item second item # first commented item third item # second commented item 

How to remove the first item that starts with # from the list?

Expected Result:

 first item second item third item # second commented item 
+4
source share
2 answers
 >>> items = ["First", "Second", "# First", "Third", "# Second"] >>> for e in items: ... if e.startswith('#'): ... items.remove(e) ... break ... >>> items ['First', 'Second', 'Third', '# Second'] 
+6
source
 items = ["First", "Second", "# First", "Third", "# Second"] for i in xrange(len(items)): if items[i][0] == '#': items.pop(i) break print items 
+1
source

Source: https://habr.com/ru/post/1339575/


All Articles