unique_everseen

class iteration_utilities.unique_everseen(iterable, key=None)

Find unique elements, preserving their order. Remembers all elements ever seen.

Parameters:

iterable : iterable

Iterable containing the elements.

key : callable, 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:

iterable : generator

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, consume
>>> 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) is 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).

seen

(Seen) Already seen values (readonly).