Skip to content

Commit

Permalink
Add unique() recipe to itertools docs (pythongh-119911)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger authored Jun 1, 2024
1 parent 7dc745d commit 63111bf
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -857,15 +857,15 @@ and :term:`generators <generator>` which incur interpreter overhead.
return len(take(2, groupby(iterable, key))) <= 1

def unique_justseen(iterable, key=None):
"List unique elements, preserving order. Remember only the element just seen."
"Yield unique elements, preserving order. Remember only the element just seen."
# unique_justseen('AAAABBBCCDAABBB') → A B C D A B
# unique_justseen('ABBcCAD', str.casefold) → A B c A D
if key is None:
return map(operator.itemgetter(0), groupby(iterable))
return map(next, map(operator.itemgetter(1), groupby(iterable, key)))

def unique_everseen(iterable, key=None):
"List unique elements, preserving order. Remember all elements ever seen."
"Yield unique elements, preserving order. Remember all elements ever seen."
# unique_everseen('AAAABBBCCDAABBB') → A B C D
# unique_everseen('ABBcCAD', str.casefold) → A B c D
seen = set()
Expand All @@ -880,6 +880,11 @@ and :term:`generators <generator>` which incur interpreter overhead.
seen.add(k)
yield element

def unique(iterable, key=None, reverse=False):
"Yield unique elements in sorted order. Supports unhashable inputs."
# unique([[1, 2], [3, 4], [1, 2]]) → [1, 2] [3, 4]
return unique_justseen(sorted(iterable, key=key, reverse=reverse), key=key)

def sliding_window(iterable, n):
"Collect data into overlapping fixed-length chunks or blocks."
# sliding_window('ABCDEFG', 4) → ABCD BCDE CDEF DEFG
Expand Down Expand Up @@ -1605,6 +1610,13 @@ The following recipes have a more mathematical flavor:
>>> ''.join(input_iterator)
'AAABBBCCDAABBB'

>>> list(unique([[1, 2], [3, 4], [1, 2]]))
[[1, 2], [3, 4]]
>>> list(unique('ABBcCAD', str.casefold))
['A', 'B', 'c', 'D']
>>> list(unique('ABBcCAD', str.casefold, reverse=True))
['D', 'c', 'B', 'A']

>>> d = dict(a=1, b=2, c=3)
>>> it = iter_except(d.popitem, KeyError)
>>> d['d'] = 4
Expand Down

0 comments on commit 63111bf

Please sign in to comment.