insert unique data in a list
Gary Herron
gherron at islandtraining.com
Sun Dec 13 12:07:08 EST 2009
mattia wrote:
> How can I insert non-duplicate data in a list? I mean, is there a
> particular option in the creation of a list that permit me not to use
> something like:
> def append_unique(l, val):
> if val not in l:
> l.append(val)
>
> Thanks,
> Mattia
>
Unless the insertion order is important, you could use a set -- where a
second insertion will have no effect.
>>> s = set()
>>> s.add(1)
>>> s.add(2)
>>> s.add(1)
>>> print s
set([1, 2])
Gary Herron
More information about the Python-list
mailing list