src/pyams_utils/list.py
changeset 61 6a579f05c692
parent 1 3f89629b9e54
child 72 9049384a2bd4
equal deleted inserted replaced
60:cde349864894 61:6a579f05c692
    21 
    21 
    22 
    22 
    23 def unique(seq, idfun=None):
    23 def unique(seq, idfun=None):
    24     """Extract unique values from list, preserving order
    24     """Extract unique values from list, preserving order
    25 
    25 
       
    26     Original list is not modified.
       
    27 
    26     >>> from pyams_utils.list import unique
    28     >>> from pyams_utils.list import unique
    27     >>> mylist = [1, 2, 3, 2, 1]
    29     >>> mylist = [1, 2, 3, 2, 1]
    28     >>> unique(mylist)
    30     >>> unique(mylist)
    29     [1, 2, 3]
    31     [1, 2, 3]
       
    32 
       
    33     >>> mylist = [3, 2, 2, 1, 4, 2]
       
    34     >>> unique(mylist)
       
    35     [3, 2, 1, 4]
       
    36 
       
    37     You can also set an 'id' function applied on each element:
       
    38     >>> mylist = [1, 2, 3, '2', 4]
       
    39     >>> unique(mylist, idfun=str)
       
    40     [1, 2, 3, 4]
    30     """
    41     """
    31     if idfun is None:
    42     if idfun is None:
    32         def idfun(x): return x
    43         def idfun(x): return x
    33     seen = {}
    44     seen = {}
    34     result = []
    45     result = []