# HG changeset patch # User Thierry Florac # Date 1530514676 -7200 # Node ID c110fd0251ff4e564392c4d32e678aa38971f747 # Parent 48a13f7bddbe6dc68b14f338e79d4fd7f17db362 Updated docstring diff -r 48a13f7bddbe -r c110fd0251ff src/pyams_utils/list.py --- a/src/pyams_utils/list.py Mon Jul 02 08:50:10 2018 +0200 +++ b/src/pyams_utils/list.py Mon Jul 02 08:57:56 2018 +0200 @@ -25,7 +25,7 @@ """Extract unique values from list, preserving order :param iterator seq: input list - :param callable idfun: an identity function which is used to get 'identity' value of each element + :param callable key: an identity function which is used to get 'identity' value of each element in the list :return: list; a new list containing only unique elements of the original list in their initial order. Original list is not modified. @@ -65,9 +65,31 @@ def unique_iter(iterable, key=None): - """List unique elements, preserving order. Remember all elements ever seen.""" - # unique_everseen('AAAABBBCCDAABBB') --> A B C D - # unique_everseen('ABBCcAD', str.lower) --> A B C D + """Iterate over iterator values, yielding only unique values + + :param iterator iterable: input iterator + :param callable key: an identity function which is used to get 'identity' value of each element + in the list + :return: an iterator of unique values + + >>> from pyams_utils.list import unique_iter + >>> mylist = [1, 2, 3, 2, 1] + >>> list(unique_iter(mylist)) + [1, 2, 3] + + >>> mylist = [3, 2, 2, 1, 4, 2] + >>> list(unique_iter(mylist)) + [3, 2, 1, 4] + + You can also set an 'id' function applied on each element: + + >>> mylist = [1, 2, 3, '2', 4] + >>> list(unique_iter(mylist, key=str)) + [1, 2, 3, 4] + >>> mylist = ['A', 'B', 'b', '2', 4] + >>> list(unique_iter(mylist, key=lambda x: str(x).lower())) + ['A', 'B', '2', 4] + """ seen = set() seen_add = seen.add if key is None: