how to remove multiple occurrences of a string within a list?
Steven Bethard
steven.bethard at gmail.com
Tue Apr 3 18:05:19 EDT 2007
bahoo wrote:
> The larger problem is, I have a list of strings that I want to remove
> from another list of strings.
If you don't care about the resulting order::
>>> items = ['foo', 'bar', 'baz', 'bar', 'foo', 'frobble']
>>> to_remove = ['foo', 'bar']
>>> set(items) - set(to_remove)
set(['frobble', 'baz'])
If you do care about the resulting order::
>>> to_remove = set(to_remove)
>>> [item for item in items if item not in to_remove]
['baz', 'frobble']
STeVe
More information about the Python-list
mailing list