duplicates¶
- class iteration_utilities.duplicates(iterable, key=None)¶
Return only duplicate entries, remembers all items ever seen.
- Parameters:
- iterableiterable
Iterable containing the elements.
- keycallable, optional
If given it must be a callable taking one argument and this callable is applied to the value before checking if it was seen yet.
- Returns:
- iterablegenerator
An iterable containing all duplicates values of the iterable.
Notes
The items in the iterable should implement equality.
If the items are hashable the function is much faster.
Examples
Multiple duplicates will be kept:
>>> from iteration_utilities import duplicates >>> list(duplicates('AABBCCDA')) ['A', 'B', 'C', 'A'] >>> list(duplicates('ABBCcAD', str.lower)) ['B', 'c', 'A']
To get each duplicate only once this can be combined with
unique_everseen()
:>>> from iteration_utilities import unique_everseen >>> list(unique_everseen(duplicates('AABBCCDA'))) ['A', 'B', 'C']
- key¶
(callable or None) The key function (readonly).