Entry
How can i delete list if its belong to another one, e.g ['1234\n', '34\n']
Jan 22nd, 2008 11:35
stephen brown, Mohammed Mohammed,
Well, the short answer is, the same way you delete any other element of
a list, e.g.:
>>> a = ['1234\n', '34\n']
>>> del(a[0])
>>> a
['34\n']
>>>
But your example isn't what you asked for, because a is a list of
strings, not a list of lists. Lists and strings are both sequences, but
they aren't the same, because a string is immutable, and a list is
mutable. You can easily make a string into a list, and deletion works
just as it should. Note that you can delete an element of a list
embedded in another list, or you can delete a whole list which is an
element of another list:
>>> b = [list('1234\n'), list('34\n')]
>>> del(b[0][2])
>>> b
[['1', '2', '4', '\n'], ['3', '4', '\n']]
>>> del(b[1])
>>> b
[['1', '2', '4', '\n']]
>>>