On Dec 13, 11:37 am, mattia <ger...@gmail.com> 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

You could also define a custom object that manages a custom ordered
set

class unique_set(object):
   def __init__(self,list):
       self.list = list

   def __add___(self,val):
       if val not in self.list
          self.list.append(val)
          return True
       return False

>>> unique_list = unique_set(['a','b','c'])
>>> unique_list.list
['a', 'b', 'c']
>>> unique_list + 'd'
True
>>> unique_list + 'e'
True
>>> unique_list + 'd'
False
>>> unique_list.list
['a', 'b', 'c', 'd', 'e']
>>>

I've used this on a few projects, it makes for wonderfully clean code,
because you can look at your program as an overview without all the
arithmetic behind it.

hope it helps
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to