src/pyams_utils/list.py
changeset 1 3f89629b9e54
child 61 6a579f05c692
equal deleted inserted replaced
0:16d47bd81d84 1:3f89629b9e54
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 
       
    20 # import packages
       
    21 
       
    22 
       
    23 def unique(seq, idfun=None):
       
    24     """Extract unique values from list, preserving order
       
    25 
       
    26     >>> from pyams_utils.list import unique
       
    27     >>> mylist = [1, 2, 3, 2, 1]
       
    28     >>> unique(mylist)
       
    29     [1, 2, 3]
       
    30     """
       
    31     if idfun is None:
       
    32         def idfun(x): return x
       
    33     seen = {}
       
    34     result = []
       
    35     for item in seq:
       
    36         marker = idfun(item)
       
    37         if marker in seen:
       
    38             continue
       
    39         seen[marker] = 1
       
    40         result.append(item)
       
    41     return result