0
# Comparison and Equality Operations
1
2
Functions for comparing and testing equality between iterables.
3
4
## Capabilities
5
6
### Iterator Equality Testing
7
8
Functions for testing equality between iterators and iterables.
9
10
```python { .api }
11
def iequals(*iterables: Iterable[Any]) -> bool: ...
12
```
13
14
**Usage:**
15
16
```python
17
from more_itertools import iequals
18
19
# Test if multiple iterables produce the same elements
20
iequals([1, 2, 3], (1, 2, 3), iter([1, 2, 3])) # True
21
iequals([1, 2, 3], [1, 2, 4]) # False
22
23
# Works with generators and any iterables
24
def gen1():
25
yield from [1, 2, 3]
26
27
def gen2():
28
yield from [1, 2, 3]
29
30
iequals(gen1(), gen2()) # True
31
32
# Different lengths
33
iequals([1, 2, 3], [1, 2]) # False
34
```
35
36
### Difference Detection
37
38
Functions for finding differences between iterables.
39
40
```python { .api }
41
def difference(iterable: Iterable[Any], *others: Iterable[Any]) -> Iterator[Any]: ...
42
```
43
44
**Usage:**
45
46
```python
47
from more_itertools import difference
48
49
# Find elements in first iterable not in others
50
first = [1, 2, 3, 4, 5]
51
second = [3, 4, 5, 6, 7]
52
third = [5, 6, 7, 8, 9]
53
54
diff = list(difference(first, second, third)) # [1, 2]
55
56
# Order matters - finds elements in first that aren't in any of the others
57
diff2 = list(difference(second, first)) # [6, 7]
58
59
# With strings
60
text1 = "hello world"
61
text2 = "world hello"
62
char_diff = ''.join(difference(text1, text2)) # "" (same characters)
63
64
text3 = "hello world!"
65
char_diff2 = ''.join(difference(text3, text1)) # "!"
66
```
67
68
### Conditional Splitting
69
70
Functions for splitting iterables based on predicates.
71
72
```python { .api }
73
def before_and_after(predicate: Callable[[Any], bool], iterable: Iterable[Any]) -> tuple[Iterator[Any], Iterator[Any]]: ...
74
```
75
76
**Usage:**
77
78
```python
79
from more_itertools import before_and_after
80
81
# Split iterable at first element matching predicate
82
data = [1, 2, 3, 10, 4, 5, 6]
83
84
before, after = before_and_after(lambda x: x >= 10, data)
85
86
before_list = list(before) # [1, 2, 3]
87
after_list = list(after) # [10, 4, 5, 6]
88
89
# With strings - split at first vowel
90
text = "programming"
91
before_vowel, from_vowel = before_and_after(lambda c: c in 'aeiou', text)
92
93
print(''.join(before_vowel)) # "pr"
94
print(''.join(from_vowel)) # "ogramming"
95
96
# If predicate never matches, before gets everything, after is empty
97
no_match_data = [1, 2, 3, 4]
98
before, after = before_and_after(lambda x: x > 10, no_match_data)
99
100
list(before) # [1, 2, 3, 4]
101
list(after) # []
102
```