@@ -6,8 +6,8 @@ The `operator` module provides function equivalents for Python's operators, usef
66
77| Operation | Time | Space | Notes |
88| -----------| ------| -------| -------|
9- | ` add(a, b) ` | O(1) | O(1) | Addition |
10- | ` mul(a, b) ` | O(1) | O(1) | Multiplication |
9+ | ` add(a, b) ` | Varies | Varies | Same as ` a + b ` |
10+ | ` mul(a, b) ` | Varies | Varies | Same as ` a * b ` |
1111| ` itemgetter(key) ` | O(1) | O(1) | Get item function |
1212| ` attrgetter(name) ` | O(1) | O(1) | Get attribute function |
1313| ` methodcaller(name) ` | O(1) | O(1) | Call method function |
@@ -16,12 +16,12 @@ The `operator` module provides function equivalents for Python's operators, usef
1616
1717### Basic Operations
1818
19- #### Time Complexity: O(1)
19+ #### Time Complexity: Varies by operand type
2020
2121``` python
2222import operator
2323
24- # Arithmetic: O(1)
24+ # Arithmetic: same as a + b, etc.
2525result = operator.add(5 , 3 ) # 8
2626result = operator.sub(10 , 4 ) # 6
2727result = operator.mul(6 , 7 ) # 42
@@ -35,22 +35,22 @@ numbers = [1, 2, 3, 4, 5]
3535doubled = list (map (operator.mul, numbers, [2 ] * len (numbers))) # O(n)
3636```
3737
38- #### Space Complexity: O(1)
38+ #### Space Complexity: Varies by operand type
3939
4040``` python
4141import operator
4242
43- result = operator.add(a, b) # O(1) space
43+ result = operator.add(a, b) # Same as a + b
4444```
4545
4646## Comparison Operators
4747
48- #### Time Complexity: O(1)
48+ #### Time Complexity: Varies by operand type
4949
5050``` python
5151import operator
5252
53- # Comparisons: O(1)
53+ # Comparisons: same as a == b, a < b, etc.
5454operator.eq(5 , 5 ) # True
5555operator.ne(5 , 3 ) # True
5656operator.lt(3 , 5 ) # True
@@ -59,7 +59,7 @@ operator.gt(5, 3) # True
5959operator.ge(5 , 3 ) # True
6060
6161# With sorted: O(n log n)
62- items = [5 , 2 , 8 , 1 , 9 ]
62+ items = [( 5 ,), ( 2 ,), ( 8 ,), ( 1 ,), ( 9 ,) ]
6363sorted_items = sorted (items, key = operator.itemgetter(0 )) # O(n log n)
6464```
6565
@@ -68,14 +68,14 @@ sorted_items = sorted(items, key=operator.itemgetter(0)) # O(n log n)
6868``` python
6969import operator
7070
71- result = operator.eq(a, b) # O(1) space
71+ result = operator.eq(a, b) # Same as a == b
7272```
7373
7474## Item and Attribute Access
7575
7676### itemgetter()
7777
78- #### Time Complexity: O(1)
78+ #### Time Complexity: Depends on the called method
7979
8080``` python
8181import operator
@@ -152,7 +152,7 @@ import operator
152152caller = operator.methodcaller(' upper' )
153153result = caller(' hello' ) # 'HELLO' - O(n) for string
154154
155- # With arguments: O(1)
155+ # With arguments: still depends on method complexity
156156caller = operator.methodcaller(' replace' , ' a' , ' b' )
157157result = caller(' banana' ) # 'bbnbnb' - O(n) for string
158158
@@ -162,13 +162,13 @@ uppers = list(map(operator.methodcaller('upper'), strings)) # O(n*m)
162162# Result: ['HELLO', 'WORLD', 'PYTHON']
163163```
164164
165- #### Space Complexity: O(m)
165+ #### Space Complexity: Depends on the called method
166166
167167``` python
168168import operator
169169
170170caller = operator.methodcaller(' upper' ) # O(1) space for method ref
171- result = caller(string) # O(m) space for result
171+ result = caller(string) # Same as string.upper()
172172```
173173
174174## In-Place Operations
@@ -266,9 +266,9 @@ import operator
266266items = [5 , 2 , 8 , 1 , 9 ]
267267count_gt_5 = sum (map (operator.gt, items, [5 ] * len (items))) # O(n)
268268
269- # Apply to sequences: O(n*m )
269+ # Apply to sequences: O(n)
270270data = {' a' : 1 , ' b' : 2 , ' c' : 3 }
271- values = list (map (operator.itemgetter, data.keys( ), [ data] * len (data ))) # O(n)
271+ values = list (map (operator.itemgetter( 1 ), data.items( ))) # O(n)
272272```
273273
274274## Performance Characteristics
0 commit comments