Seen¶
- class iteration_utilities.Seen(seenset=None, seenlist=None)¶
Helper class which adds the items after each
contains_add()
check.- Parameters:
Examples
This class adds each item after
contains_add()
call but also supports normalin
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
,==
,!=
andlen
. It is mostly included because it unified the code induplicates()
,unique_everseen()
, andall_distinct()
and might be useful in other applications.- 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:
- contained
bool
True
if o is contained in self otherwiseFalse
.
- contained
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})