Q:

Find and remove list from list

I've been trying to find some post that has done what i'm trying to do, but i can't find anything.
I've got this list, i.e.
rows =
[['Jan 31', ' 2014 19:48:30.096606000', '0x10', '0x00000000', '0x0f7864ef', '0x0f7864f1', '', 'blahblah', 'other \n'], 
['Jan 31', ' 2014 19:48:30.829329000', '0x10', '0x00000000', '0x0f920978', '0x0f92097a', '', 'blahblah', 'anotherr \n']]

i need to find AND remove a list from the list searching by the 5th item like this:
search == '0x0f7864ef'
if any(e[4] == search for e in rows):

If the searched var exists then i get a True, but I don't know how to remove it from "rows". And doing something like rows.remove(e) just returns an error
I have tried looping over a set and removing when found, but i got an error. Also, I would rather not to have to loop through the set/list. This is what i tried:
>>> a = {( '1','da','vi' ), (2,'be','vi') }
>>> for item in a:
...   if 'da' in item:
...     a.remove(item)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Set changed size during iteration
>>> a
set([(2, 'be', 'vi')])

Even though the item was removed, I got an error...
Thanks!

A:

Just create a new list with the items filtered, like this:
new_list = [item for item in rows if search not in item]

This is a list comprehension, which is an expression that evaluates as a list. The longer way to write the above is in a for + if loop, like this:
new_list = []
for item in rows:
   if search not in item:
       new_list.append(item)

It is very poor practice to modify a list which you are looping over, which is why the standard practice is to create a new list; either with a list comprehension or with a more traditional loop.

A:

You should never insert/remove elements in a list while you're iterating on it.
Instead you could use a higher-order function, like filter.
In Python 2.7:
>>> filter(lambda e: e[4] != '0x0f7864ef', rows)
[['Jan 31',
  ' 2014 19:48:30.829329000',
  '0x10',
  '0x00000000',
  '0x0f920978',
  '0x0f92097a',
  '',
  'blahblah',
  'anotherr \n']]

In Python 3.x (filter returns a generator):
>>> filter(lambda e: e[4] != '0x0f7864ef', rows)
<builtins.filter at 0x7f76e2432810>

>>> list(filter(lambda e: e[4] != '0x0f7864ef', rows))
[['Jan 31',
  ' 2014 19:48:30.829329000',
  '0x10',
  '0x00000000',
  '0x0f920978',
  '0x0f92097a',
  '',
  'blahblah',
  'anotherr \n']]

For the sake of readibility you might prefer to define a named function instead of using a lambda.

