how to remove multiple occurrences of a string within a list?

bahoo b83503104 at yahoo.com
Tue Apr 3 17:52:13 EDT 2007


On Apr 3, 4:21 pm, Steve Holden <s... at holdenweb.com> wrote:
> bahoo wrote:
> > On Apr 3, 2:31 pm, "Matimus" <mccre... at gmail.com> wrote:
> >> It depends on your application, but a 'set' might really be what you
> >> want, as opposed to a list.
>
> >>>>> s = set(["0024","haha","0024"])
> >>>>> s
> >> set(["0024","haha"])>>> s.remove("0024")
> >>>>> s
> >> set(["haha"])
>
> > This sounds cool.
> > But is there a command I can convert the "set" back to a "list"?
>
> That would be list(). So what you want is
>
> s = set(["0024","haha","0024"])
> s.remove("0024")
> l = list(s)
>
> or something like it. It seems, a priori, unlikely that you only want to
> remove items with that specific value, Is this part of some larger problem?
>
> regards
>   Steve
> --
> Steve Holden       +44 150 684 7255  +1 800 494 3119
> Holden Web LLC/Ltd          http://www.holdenweb.com
> Skype: holdenweb    http://del.icio.us/steve.holden
> Recent Ramblings      http://holdenweb.blogspot.com

Thanks for all the suggestions.
The larger problem is, I have a list of strings that I want to remove
from another list of strings.
So I guess what I will do is, use a for loop, and within the for loop,
do the "list" to "set" and then back to "list".






More information about the Python-list mailing list