Seen

class iteration_utilities.Seen(seenset=None, seenlist=None)

Helper class which adds the items after each contains_add() check.

Parameters:
seensetset or None, optional

A set containing initial values.

seenlistlist or None, optional

A list containing only unhashable initial values.

Note

The seenlist should not contain hashable values (these will be ignored for all practical purposes)!

Examples

This class adds each item after contains_add() call but also supports normal in operations:

>>> from iteration_utilities import Seen
>>> x = Seen()
>>> # normal "in" operations do not add the element to the instance
>>> 1 in x
False
>>> 1 in x
False

>>> # "contains_add" checks if the item is contained but also adds it
>>> x.contains_add(2)
False
>>> x.contains_add(2)
True
>>> x
iteration_utilities.Seen({2})

>>> x.contains_add([1, 2])
False
>>> [1, 2] in x
True
>>> x
iteration_utilities.Seen({2}, seenlist=[[1, 2]])

This class does only support in, ==, != and len. It is mostly included because it unified the code in duplicates(), unique_everseen(), and all_distinct() and might be useful in other applications.

__contains__(x)

Returns if Seen contains x; either in seenset if x is hashable or seenlist if not.

__len__()

Returns the number of items in seenset and seenlist.

__eq__(other)

Check if the other Seen instance contains the same elements.

__ne__(other)

Check if the other Seen instance contains different elements.

contains_add(o, /)

Check if o is already contained in self and return the result. But also adds o to self if it’s not contained.

Parameters:
oany type

The object to check if it’s contained in self and added to self if not.

Returns:
containedbool

True if o is contained in self otherwise False.

Examples

A simple example:

>>> from iteration_utilities import Seen
>>> x = Seen()
>>> 10 in x
False
>>> x.contains_add(10)
False
>>> 10 in x
True
>>> x.contains_add(10)
True
>>> x
iteration_utilities.Seen({10})
seenlist

(list or None) The (unhashable) seen values (readonly).

Added in version 0.6.

seenset

(set) The (hashable) seen values (readonly).

Added in version 0.6.