Pydash: Python Utility Library
Pydash is a Python port of the JavaScript utility library Lo-Dash, providing a comprehensive set of functional programming helpers for collections, objects, strings, and more. It aims to simplify common programming tasks with a functional, declarative style. The current version is 8.0.6, and it follows an infrequent release cadence, often aligning with major Lo-Dash updates or significant Python ecosystem changes.
Warnings
- breaking Breaking change in v8.0.0: Functions that conflict with Python's built-in names (e.g., `map`, `filter`, `sum`, `max`, `min`) were renamed with a trailing underscore (e.g., `map_`, `filter_`).
- gotcha Aliasing `pydash` as `_` (e.g., `import pydash as _`) can lead to conflicts with other Python libraries that commonly use `_` for different purposes, such as `gettext` for internationalization or `collections.Counter` internal alias.
- gotcha While pydash embraces a functional style, not all operations are strictly immutable. Some functions, like `set_` on objects, can modify the object in place, while others return new collections. This can be a source of confusion for users expecting pure functional immutability.
Install
-
pip install pydash
Imports
- pydash
import pydash
- get
from pydash import get
- map_
from pydash import map_
Quickstart
import pydash
data = {
'user': 'fred',
'age': 40,
'active': False,
'posts': [
{'title': 'Post 1', 'tags': ['news', 'tech']},
{'title': 'Post 2', 'tags': ['coding', 'python']}
]
}
# Get a deeply nested value
post_tag = pydash.get(data, 'posts[0].tags[1]')
print(f"Deeply nested tag: {post_tag}")
# Filter a list of dictionaries
python_posts = pydash.filter_(data['posts'], lambda x: 'python' in x['tags'])
print(f"Posts about Python: {python_posts}")
# Chain operations for a more functional style
chained_result = pydash.chain(data['posts']) \
.filter_(lambda x: 'python' in x['tags']) \
.map_('title') \
.value()
print(f"Chained operation result: {chained_result}")