0
# PyDash - Python Utility Library
1
2
PyDash is a comprehensive Python port of Lo-Dash (Lodash) that provides functional programming helpers and utilities for Python. It offers a complete suite of 365 functions for manipulating arrays, objects, collections, strings, and more, following functional programming paradigms.
3
4
## Package Information
5
6
- **Package Name**: pydash
7
- **Package Type**: pypi
8
- **Language**: Python
9
- **Installation**: `pip install pydash`
10
- **Version**: 8.0.5
11
- **Total Functions**: 365 public functions across 8 main modules
12
- **Type Hints**: Full type annotation support throughout
13
- **Python Compatibility**: Modern Python with comprehensive type checking
14
15
## Installation
16
17
```python
18
pip install pydash
19
```
20
21
## Core Imports
22
23
### Functional Style Import
24
25
```python { .api }
26
import pydash as _
27
28
# Use functions directly
29
result = _.chunk([1, 2, 3, 4, 5], 2)
30
# [[1, 2], [3, 4], [5]]
31
```
32
33
### Selective Imports
34
35
```python { .api }
36
from pydash import chunk, compact, map_, filter_
37
from pydash import get, set_, has
38
from pydash import camel_case, kebab_case
39
```
40
41
### Chaining Style Import
42
43
```python { .api }
44
from pydash import chain, _
45
46
# Method chaining
47
result = _([1, 2, 3, 4, 5, 6]).filter(lambda x: x % 2 == 0).map(lambda x: x * 2).value()
48
# [4, 8, 12]
49
50
# Or using chain() function
51
result = chain([1, 2, 3, 4, 5, 6]).filter(lambda x: x % 2 == 0).map(lambda x: x * 2).value()
52
```
53
54
### Direct Module Imports
55
56
```python { .api }
57
from pydash.arrays import flatten, uniq
58
from pydash.objects import get, merge
59
from pydash.strings import snake_case, title_case
60
from pydash.collections import group_by, sort_by
61
```
62
63
## Basic Usage Examples
64
65
### Array Manipulation
66
67
```python { .api }
68
import pydash as _
69
70
# Group arrays into chunks
71
_.chunk([1, 2, 3, 4, 5, 6], 3)
72
# [[1, 2, 3], [4, 5, 6]]
73
74
# Remove falsey values
75
_.compact([0, 1, False, 2, '', 3, None])
76
# [1, 2, 3]
77
78
# Find unique values
79
_.uniq([1, 2, 1, 3, 2, 4])
80
# [1, 2, 3, 4]
81
82
# Flatten nested arrays
83
_.flatten_deep([1, [2, [3, [4]]]])
84
# [1, 2, 3, 4]
85
```
86
87
### Object Operations
88
89
```python { .api }
90
import pydash as _
91
92
user = {
93
'name': {'first': 'John', 'last': 'Doe'},
94
'age': 30,
95
'active': True
96
}
97
98
# Safe property access
99
_.get(user, 'name.first') # 'John'
100
_.get(user, 'name.middle', 'N/A') # 'N/A'
101
102
# Set nested properties
103
_.set_(user, 'address.city', 'New York')
104
105
# Pick specific properties
106
_.pick(user, 'name', 'age')
107
# {'name': {'first': 'John', 'last': 'Doe'}, 'age': 30}
108
```
109
110
### Collection Processing
111
112
```python { .api }
113
import pydash as _
114
115
users = [
116
{'name': 'John', 'age': 30, 'active': True},
117
{'name': 'Jane', 'age': 25, 'active': False},
118
{'name': 'Bob', 'age': 35, 'active': True}
119
]
120
121
# Filter collections
122
active_users = _.filter_(users, {'active': True})
123
124
# Map over collections
125
names = _.map_(users, 'name') # ['John', 'Jane', 'Bob']
126
127
# Group by property
128
by_active = _.group_by(users, 'active')
129
# {True: [{'name': 'John', ...}, {'name': 'Bob', ...}], False: [{'name': 'Jane', ...}]}
130
131
# Sort by property
132
sorted_users = _.sort_by(users, 'age')
133
```
134
135
### String Transformations
136
137
```python { .api }
138
import pydash as _
139
140
# Case conversions
141
_.camel_case('hello world') # 'helloWorld'
142
_.snake_case('helloWorld') # 'hello_world'
143
_.kebab_case('Hello World') # 'hello-world'
144
_.title_case('hello world') # 'Hello World'
145
146
# String utilities
147
_.pad('abc', 8) # ' abc '
148
_.trim(' hello ') # 'hello'
149
_.truncate('This is a long string', 10) # 'This is...'
150
```
151
152
## Architecture: Chaining System
153
154
PyDash supports both functional and chaining programming styles:
155
156
### Functional Style
157
Execute operations directly on data:
158
159
```python { .api }
160
import pydash as _
161
162
result = _.map_(_.filter_([1, 2, 3, 4, 5], lambda x: x % 2 == 0), lambda x: x * 2)
163
# [4, 8]
164
```
165
166
### Chaining Style
167
Chain operations for better readability:
168
169
```python { .api }
170
from pydash import _
171
172
result = _([1, 2, 3, 4, 5]).filter(lambda x: x % 2 == 0).map(lambda x: x * 2).value()
173
# [4, 8]
174
175
# Complex chaining example
176
result = _(users)\
177
.filter({'active': True})\
178
.sort_by('age')\
179
.map('name')\
180
.take(2)\
181
.value()
182
```
183
184
### Debugging with Tap
185
186
```python { .api }
187
from pydash import _
188
189
result = _(data)\
190
.filter(predicate)\
191
.tap(print) # Debug intermediate results\
192
.map(transform)\
193
.value()
194
```
195
196
## Module Capabilities
197
198
### [Arrays](arrays.md) - 75 Functions
199
Functions for array/list manipulation and transformation.
200
201
```python { .api }
202
import pydash as _
203
204
# Core array operations
205
_.chunk([1, 2, 3, 4, 5], 2) # Group into chunks
206
_.flatten([[1, 2], [3, 4]]) # Flatten arrays
207
_.uniq([1, 2, 1, 3, 2]) # Remove duplicates
208
_.difference([1, 2, 3], [2, 3, 4]) # Array differences
209
210
# Array transformations
211
_.zip_([1, 2], ['a', 'b']) # Zip arrays: [(1, 'a'), (2, 'b')]
212
_.from_pairs([['a', 1], ['b', 2]]) # Create dict from pairs
213
```
214
215
### [Collections](collections.md) - 33 Functions
216
Functions that work on both arrays and objects.
217
218
```python { .api }
219
import pydash as _
220
221
# Collection processing
222
_.map_([1, 2, 3], lambda x: x * 2) # Transform elements
223
_.filter_([1, 2, 3, 4], lambda x: x % 2 == 0) # Filter elements
224
_.reduce_([1, 2, 3, 4], lambda acc, x: acc + x) # Reduce to single value
225
226
# Collection grouping
227
_.group_by(['one', 'two', 'three'], len) # Group by length
228
_.count_by(['A', 'B', 'A', 'C'], _.identity) # Count occurrences
229
```
230
231
### [Objects](objects.md) - 49 Functions
232
Functions for object/dictionary manipulation.
233
234
```python { .api }
235
import pydash as _
236
237
obj = {'a': {'b': {'c': 42}}}
238
239
# Property access
240
_.get(obj, 'a.b.c') # Safe nested access: 42
241
_.has(obj, 'a.b') # Check property exists: True
242
_.set_(obj, 'a.b.d', 100) # Set nested property
243
244
# Object transformation
245
_.keys({'a': 1, 'b': 2}) # Get keys: ['a', 'b']
246
_.values({'a': 1, 'b': 2}) # Get values: [1, 2]
247
_.merge({'a': 1}, {'b': 2}) # Merge objects: {'a': 1, 'b': 2}
248
```
249
250
### [Strings](strings.md) - 69 Functions
251
Comprehensive string manipulation and formatting.
252
253
```python { .api }
254
import pydash as _
255
256
# Case transformations
257
_.camel_case('foo bar') # 'fooBar'
258
_.snake_case('fooBar') # 'foo_bar'
259
_.kebab_case('Foo Bar') # 'foo-bar'
260
_.title_case('foo bar') # 'Foo Bar'
261
262
# String utilities
263
_.pad('abc', 8) # ' abc '
264
_.truncate('A long string', 10) # 'A long...'
265
_.words('Hello world!') # ['Hello', 'world']
266
```
267
268
### [Numerical](numerical.md) - 28 Functions
269
Mathematical operations and statistical functions.
270
271
```python { .api }
272
import pydash as _
273
274
# Basic math
275
_.add(6, 4) # 10
276
_.multiply(3, 4) # 12
277
_.clamp(10, 1, 5) # 5
278
279
# Statistics
280
_.mean([1, 2, 3, 4, 5]) # 3.0
281
_.sum_([1, 2, 3, 4, 5]) # 15
282
_.max_([1, 5, 3, 9, 2]) # 9
283
```
284
285
### [Functions](functions.md) - 30 Functions
286
Higher-order functions and function utilities.
287
288
```python { .api }
289
import pydash as _
290
291
# Function composition
292
add_one = lambda x: x + 1
293
multiply_two = lambda x: x * 2
294
composed = _.flow(add_one, multiply_two)
295
composed(3) # 8
296
297
# Function control
298
debounced = _.debounce(expensive_func, 100) # Debounce calls
299
throttled = _.throttle(api_call, 1000) # Throttle calls
300
once_only = _.once(initialization) # Call only once
301
```
302
303
### [Predicates](predicates.md) - 59 Functions
304
Type checking and value testing functions.
305
306
```python { .api }
307
import pydash as _
308
309
# Type checking
310
_.is_string('hello') # True
311
_.is_number(42) # True
312
_.is_empty([]) # True
313
_.is_equal({'a': 1}, {'a': 1}) # True
314
315
# Value comparison
316
_.gt(3, 1) # True
317
_.in_range(3, 2, 6) # True
318
_.is_match({'a': 1, 'b': 2}, {'a': 1}) # True
319
```
320
321
### [Utilities](utilities.md) - 38 Functions
322
General utility functions and helpers.
323
324
```python { .api }
325
import pydash as _
326
327
# Function creation
328
always_true = _.constant(True) # Function that always returns True
329
get_name = _.property_('name') # Function to get 'name' property
330
is_adult = _.matches({'age': lambda x: x >= 18}) # Matching function
331
332
# Utilities
333
_.random(1, 10) # Random number between 1-10
334
_.times(3, lambda i: i * 2) # [0, 2, 4]
335
_.unique_id('user_') # 'user_1', 'user_2', etc.
336
```
337
338
### [Chaining](chaining.md) - Method Chaining
339
Fluent interface for composing operations.
340
341
```python { .api }
342
from pydash import _, chain
343
344
# Chain multiple operations
345
result = _([1, 2, 3, 4, 5, 6])\
346
.filter(lambda x: x % 2 == 0)\
347
.map(lambda x: x ** 2)\
348
.sum_()\
349
.value() # 56
350
351
# Complex data processing
352
processed = _(raw_data)\
353
.filter({'active': True})\
354
.group_by('department')\
355
.map_values(lambda group: _.sum_by(group, 'salary'))\
356
.value()
357
```
358
359
## Key Features
360
361
### Flexible Iteratees
362
Many functions accept flexible iteratee arguments:
363
364
```python { .api }
365
users = [{'name': 'John', 'age': 30}, {'name': 'Jane', 'age': 25}]
366
367
# Function iteratee
368
_.map_(users, lambda u: u['name']) # ['John', 'Jane']
369
370
# String property iteratee
371
_.map_(users, 'name') # ['John', 'Jane']
372
373
# Dict matcher iteratee
374
_.filter_(users, {'age': 30}) # [{'name': 'John', 'age': 30}]
375
```
376
377
### Safe Operations
378
Built-in error handling for common operations:
379
380
```python { .api }
381
import pydash as _
382
383
# Safe property access - no KeyError
384
_.get({'a': 1}, 'b.c.d', 'default') # 'default'
385
386
# Safe function application
387
_.attempt(risky_function, arg1, arg2) # Returns result or exception
388
```
389
390
### Type Safety
391
Comprehensive type hints for better development experience:
392
393
```python { .api }
394
from pydash import chunk, get
395
from typing import List, Any
396
397
# Full type support
398
chunks: List[List[int]] = chunk([1, 2, 3, 4], 2)
399
value: Any = get({'a': {'b': 1}}, 'a.b')
400
```
401
402
This documentation covers all 365 functions across PyDash's 8 main modules. Each module page provides detailed API documentation with parameters, return types, and usage examples for every function.