Removing list elements - was:: [Tutor] Printing two elements in a list
Alan Gauld
alan.gauld at freenet.co.uk
Tue Dec 7 23:42:50 CET 2004
> I know I am wrong here because I do not know how to
> search and remove an element in a list. Can any one
> please help me.
This is what the filter function is for...
But you can use list comprehensions too:
[element for element in list if element not foo]
so in your case:
lst = f.readlines() # get file into a list
lst = [line for line in lst if not line.startswith('Name=')]
Or something very similar. For more control use a regular
expression to filter the lines
Alan G.
More information about the Tutor
mailing list