A comprehensive Python utility library for functional programming inspired by JavaScript's Lo-Dash
npx @tessl/cli install tessl/pypi-pydash@8.0.0PyDash 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.
pip install pydashpip install pydashimport pydash as _
# Use functions directly
result = _.chunk([1, 2, 3, 4, 5], 2)
# [[1, 2], [3, 4], [5]]from pydash import chunk, compact, map_, filter_
from pydash import get, set_, has
from pydash import camel_case, kebab_casefrom pydash import chain, _
# Method chaining
result = _([1, 2, 3, 4, 5, 6]).filter(lambda x: x % 2 == 0).map(lambda x: x * 2).value()
# [4, 8, 12]
# Or using chain() function
result = chain([1, 2, 3, 4, 5, 6]).filter(lambda x: x % 2 == 0).map(lambda x: x * 2).value()from pydash.arrays import flatten, uniq
from pydash.objects import get, merge
from pydash.strings import snake_case, title_case
from pydash.collections import group_by, sort_byimport pydash as _
# Group arrays into chunks
_.chunk([1, 2, 3, 4, 5, 6], 3)
# [[1, 2, 3], [4, 5, 6]]
# Remove falsey values
_.compact([0, 1, False, 2, '', 3, None])
# [1, 2, 3]
# Find unique values
_.uniq([1, 2, 1, 3, 2, 4])
# [1, 2, 3, 4]
# Flatten nested arrays
_.flatten_deep([1, [2, [3, [4]]]])
# [1, 2, 3, 4]import pydash as _
user = {
'name': {'first': 'John', 'last': 'Doe'},
'age': 30,
'active': True
}
# Safe property access
_.get(user, 'name.first') # 'John'
_.get(user, 'name.middle', 'N/A') # 'N/A'
# Set nested properties
_.set_(user, 'address.city', 'New York')
# Pick specific properties
_.pick(user, 'name', 'age')
# {'name': {'first': 'John', 'last': 'Doe'}, 'age': 30}import pydash as _
users = [
{'name': 'John', 'age': 30, 'active': True},
{'name': 'Jane', 'age': 25, 'active': False},
{'name': 'Bob', 'age': 35, 'active': True}
]
# Filter collections
active_users = _.filter_(users, {'active': True})
# Map over collections
names = _.map_(users, 'name') # ['John', 'Jane', 'Bob']
# Group by property
by_active = _.group_by(users, 'active')
# {True: [{'name': 'John', ...}, {'name': 'Bob', ...}], False: [{'name': 'Jane', ...}]}
# Sort by property
sorted_users = _.sort_by(users, 'age')import pydash as _
# Case conversions
_.camel_case('hello world') # 'helloWorld'
_.snake_case('helloWorld') # 'hello_world'
_.kebab_case('Hello World') # 'hello-world'
_.title_case('hello world') # 'Hello World'
# String utilities
_.pad('abc', 8) # ' abc '
_.trim(' hello ') # 'hello'
_.truncate('This is a long string', 10) # 'This is...'PyDash supports both functional and chaining programming styles:
Execute operations directly on data:
import pydash as _
result = _.map_(_.filter_([1, 2, 3, 4, 5], lambda x: x % 2 == 0), lambda x: x * 2)
# [4, 8]Chain operations for better readability:
from pydash import _
result = _([1, 2, 3, 4, 5]).filter(lambda x: x % 2 == 0).map(lambda x: x * 2).value()
# [4, 8]
# Complex chaining example
result = _(users)\
.filter({'active': True})\
.sort_by('age')\
.map('name')\
.take(2)\
.value()from pydash import _
result = _(data)\
.filter(predicate)\
.tap(print) # Debug intermediate results\
.map(transform)\
.value()Functions for array/list manipulation and transformation.
import pydash as _
# Core array operations
_.chunk([1, 2, 3, 4, 5], 2) # Group into chunks
_.flatten([[1, 2], [3, 4]]) # Flatten arrays
_.uniq([1, 2, 1, 3, 2]) # Remove duplicates
_.difference([1, 2, 3], [2, 3, 4]) # Array differences
# Array transformations
_.zip_([1, 2], ['a', 'b']) # Zip arrays: [(1, 'a'), (2, 'b')]
_.from_pairs([['a', 1], ['b', 2]]) # Create dict from pairsFunctions that work on both arrays and objects.
import pydash as _
# Collection processing
_.map_([1, 2, 3], lambda x: x * 2) # Transform elements
_.filter_([1, 2, 3, 4], lambda x: x % 2 == 0) # Filter elements
_.reduce_([1, 2, 3, 4], lambda acc, x: acc + x) # Reduce to single value
# Collection grouping
_.group_by(['one', 'two', 'three'], len) # Group by length
_.count_by(['A', 'B', 'A', 'C'], _.identity) # Count occurrencesFunctions for object/dictionary manipulation.
import pydash as _
obj = {'a': {'b': {'c': 42}}}
# Property access
_.get(obj, 'a.b.c') # Safe nested access: 42
_.has(obj, 'a.b') # Check property exists: True
_.set_(obj, 'a.b.d', 100) # Set nested property
# Object transformation
_.keys({'a': 1, 'b': 2}) # Get keys: ['a', 'b']
_.values({'a': 1, 'b': 2}) # Get values: [1, 2]
_.merge({'a': 1}, {'b': 2}) # Merge objects: {'a': 1, 'b': 2}Comprehensive string manipulation and formatting.
import pydash as _
# Case transformations
_.camel_case('foo bar') # 'fooBar'
_.snake_case('fooBar') # 'foo_bar'
_.kebab_case('Foo Bar') # 'foo-bar'
_.title_case('foo bar') # 'Foo Bar'
# String utilities
_.pad('abc', 8) # ' abc '
_.truncate('A long string', 10) # 'A long...'
_.words('Hello world!') # ['Hello', 'world']Mathematical operations and statistical functions.
import pydash as _
# Basic math
_.add(6, 4) # 10
_.multiply(3, 4) # 12
_.clamp(10, 1, 5) # 5
# Statistics
_.mean([1, 2, 3, 4, 5]) # 3.0
_.sum_([1, 2, 3, 4, 5]) # 15
_.max_([1, 5, 3, 9, 2]) # 9Higher-order functions and function utilities.
import pydash as _
# Function composition
add_one = lambda x: x + 1
multiply_two = lambda x: x * 2
composed = _.flow(add_one, multiply_two)
composed(3) # 8
# Function control
debounced = _.debounce(expensive_func, 100) # Debounce calls
throttled = _.throttle(api_call, 1000) # Throttle calls
once_only = _.once(initialization) # Call only onceType checking and value testing functions.
import pydash as _
# Type checking
_.is_string('hello') # True
_.is_number(42) # True
_.is_empty([]) # True
_.is_equal({'a': 1}, {'a': 1}) # True
# Value comparison
_.gt(3, 1) # True
_.in_range(3, 2, 6) # True
_.is_match({'a': 1, 'b': 2}, {'a': 1}) # TrueGeneral utility functions and helpers.
import pydash as _
# Function creation
always_true = _.constant(True) # Function that always returns True
get_name = _.property_('name') # Function to get 'name' property
is_adult = _.matches({'age': lambda x: x >= 18}) # Matching function
# Utilities
_.random(1, 10) # Random number between 1-10
_.times(3, lambda i: i * 2) # [0, 2, 4]
_.unique_id('user_') # 'user_1', 'user_2', etc.Fluent interface for composing operations.
from pydash import _, chain
# Chain multiple operations
result = _([1, 2, 3, 4, 5, 6])\
.filter(lambda x: x % 2 == 0)\
.map(lambda x: x ** 2)\
.sum_()\
.value() # 56
# Complex data processing
processed = _(raw_data)\
.filter({'active': True})\
.group_by('department')\
.map_values(lambda group: _.sum_by(group, 'salary'))\
.value()Many functions accept flexible iteratee arguments:
users = [{'name': 'John', 'age': 30}, {'name': 'Jane', 'age': 25}]
# Function iteratee
_.map_(users, lambda u: u['name']) # ['John', 'Jane']
# String property iteratee
_.map_(users, 'name') # ['John', 'Jane']
# Dict matcher iteratee
_.filter_(users, {'age': 30}) # [{'name': 'John', 'age': 30}]Built-in error handling for common operations:
import pydash as _
# Safe property access - no KeyError
_.get({'a': 1}, 'b.c.d', 'default') # 'default'
# Safe function application
_.attempt(risky_function, arg1, arg2) # Returns result or exceptionComprehensive type hints for better development experience:
from pydash import chunk, get
from typing import List, Any
# Full type support
chunks: List[List[int]] = chunk([1, 2, 3, 4], 2)
value: Any = get({'a': {'b': 1}}, 'a.b')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.