or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-utilities.mdcombinatorics.mdcombining.mdcomparison.mdgrouping.mdindex.mdindexing.mditeration-utilities.mdlookahead.mdmathematical.mdrandom-operations.mdselecting.mdsequence-utilities.mdspecial-purpose.mdsummarizing.mduniqueness.mdutility-classes.mdwindowing.md

uniqueness.mddocs/

0

# Uniqueness Operations

1

2

Functions for handling unique elements and finding duplicates.

3

4

## Capabilities

5

6

### Basic Uniqueness

7

8

Remove duplicates and find unique elements.

9

10

```python { .api }

11

def unique_everseen(iterable, key=None):

12

"""

13

Yield unique items preserving order of first occurrence.

14

15

Args:

16

iterable: Input iterable

17

key: Optional key function for uniqueness comparison

18

19

Returns:

20

Iterator of unique items in original order

21

"""

22

23

def unique_justseen(iterable, key=None):

24

"""

25

Remove consecutive duplicate items.

26

27

Args:

28

iterable: Input iterable

29

key: Optional key function for comparison

30

31

Returns:

32

Iterator with consecutive duplicates removed

33

"""

34

35

def unique(iterable, key=None, reverse=False):

36

"""

37

Remove duplicates from sorted iterable.

38

39

Args:

40

iterable: Input iterable (should be sorted)

41

key: Optional key function for comparison

42

reverse: If True, reverse the order

43

44

Returns:

45

Iterator of unique items

46

"""

47

```

48

49

**Usage Examples:**

50

51

```python

52

from more_itertools import unique_everseen, unique_justseen

53

54

# Preserve order, remove all duplicates

55

result = list(unique_everseen([1, 2, 2, 3, 1, 4]))

56

# Result: [1, 2, 3, 4]

57

58

# Remove only consecutive duplicates

59

result = list(unique_justseen([1, 1, 2, 2, 3, 1, 1]))

60

# Result: [1, 2, 3, 1]

61

62

# With key function

63

result = list(unique_everseen(['a', 'A', 'b', 'B'], key=str.lower))

64

# Result: ['a', 'b']

65

```

66

67

### Duplicate Detection

68

69

Find and extract duplicate elements.

70

71

```python { .api }

72

def duplicates_everseen(iterable, key=None):

73

"""

74

Yield duplicate items preserving order of first duplication.

75

76

Args:

77

iterable: Input iterable

78

key: Optional key function for comparison

79

80

Returns:

81

Iterator of items that appear more than once

82

"""

83

84

def duplicates_justseen(iterable, key=None):

85

"""

86

Yield consecutive duplicate items.

87

88

Args:

89

iterable: Input iterable

90

key: Optional key function for comparison

91

92

Returns:

93

Iterator of consecutive duplicate items

94

"""

95

```

96

97

**Usage Examples:**

98

99

```python

100

from more_itertools import duplicates_everseen, duplicates_justseen

101

102

# Find all duplicates

103

duplicates = list(duplicates_everseen([1, 2, 2, 3, 1, 4, 2]))

104

# Result: [2, 1, 2] (order of first duplication)

105

106

# Find consecutive duplicates

107

consecutive_dups = list(duplicates_justseen([1, 1, 2, 3, 3, 3, 4]))

108

# Result: [1, 3, 3]

109

```

110

111

### Uniqueness Testing

112

113

Test and classify uniqueness properties.

114

115

```python { .api }

116

def all_unique(iterable, key=None):

117

"""

118

Check if all items in iterable are unique.

119

120

Args:

121

iterable: Input iterable to test

122

key: Optional key function for comparison

123

124

Returns:

125

True if all items are unique, False otherwise

126

"""

127

128

def classify_unique(iterable, key=None):

129

"""

130

Classify items by their uniqueness.

131

132

Args:

133

iterable: Input iterable

134

key: Optional key function for comparison

135

136

Returns:

137

Tuple of (unique_items, duplicated_items) lists

138

"""

139

```

140

141

**Usage Examples:**

142

143

```python

144

from more_itertools import all_unique, classify_unique

145

146

# Test uniqueness

147

print(all_unique([1, 2, 3, 4])) # True

148

print(all_unique([1, 2, 2, 3])) # False

149

150

# Classify by uniqueness

151

unique_items, duplicate_items = classify_unique([1, 2, 2, 3, 4, 4, 5])

152

print(unique_items) # [1, 3, 5]

153

print(duplicate_items) # [2, 4]

154

```

155

156

### Set-based Uniqueness

157

158

Operations for uniqueness across multiple iterables.

159

160

```python { .api }

161

def unique_to_each(iterables):

162

"""

163

Find items unique to each iterable.

164

165

Args:

166

iterables: Sequence of iterables to compare

167

168

Returns:

169

Iterator of sets, each containing items unique to corresponding iterable

170

"""

171

172

def unique_in_window(iterable, n, key=None):

173

"""

174

Yield items unique within sliding window.

175

176

Args:

177

iterable: Input iterable

178

n: Window size

179

key: Optional key function for comparison

180

181

Returns:

182

Iterator of items that are unique within their window

183

"""

184

```

185

186

**Usage Examples:**

187

188

```python

189

from more_itertools import unique_to_each, unique_in_window

190

191

# Find items unique to each list

192

lists = [[1, 2, 3], [2, 3, 4], [3, 4, 5]]

193

unique_sets = list(unique_to_each(lists))

194

# Result: [{1}, {2}, {5}]

195

196

# Unique within sliding window

197

result = list(unique_in_window([1, 2, 1, 3, 2, 4], 3))

198

# Result: Items unique within each 3-item window

199

```