unique_everseen¶
- class iteration_utilities.unique_everseen(iterable, key=None)¶
Find unique elements, preserving their order. Remembers all elements 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 unique values ever seen in the iterable.
Notes
The items in the iterable should implement equality.
If the items are hashable the function is much faster.
Examples
Some simple examples:
>>> from iteration_utilities import unique_everseen >>> list(unique_everseen('AAAABBBCCDAABBB')) ['A', 'B', 'C', 'D'] >>> list(unique_everseen('ABBCcAD', str.lower)) ['A', 'B', 'C', 'D']
Even unhashable values can be processed, like list:
>>> list(unique_everseen([[1, 2], [1, 1], [1, 2]])) [[1, 2], [1, 1]]
However using
key=tuple
(to make them hashable) will be faster:>>> list(unique_everseen([[1, 2], [1, 1], [1, 2]], key=tuple)) [[1, 2], [1, 1]]
One can access the already seen values by accessing the seen attribute.
- key¶
(callable or None) The key function (readonly).