lenses
A lens library for python
What it is and what it does
Lenses is a Python library that implements functional lens patterns to safely navigate and transform nested data structures without mutating the original. It lets you compose operations like filtering, mapping, and method calls across arbitrary levels of nesting—lists, dicts, custom objects—in a single declarative expression. Instead of writing nested loops and conditionals to reach deep values and rebuild structures, you chain lens operations that describe the path and transformation, then apply them to your data.
The library is inspired by Haskell's lens ecosystem but adapted for Python's imperative style. It has no external runtime dependencies and works across modern Python versions (3.7–3.11). The main trade-off is that the lens syntax is unfamiliar to most Python developers and requires learning a new mental model; the payoff is cleaner, more composable code for complex data transformations that would otherwise require deeply nested loops.
Use it for:
- Transform deeply nested JSON or dictionary structures (e.g., API responses) without writing nested loops.
- Apply the same operation to all matching elements across multiple levels of nesting in a single expression.
- Build reusable, composable data transformations that can be combined and tested independently.
- Work with immutable data patterns in functional Python code or when mutation safety is required.
- Extract and modify specific fields in complex object hierarchies without intermediate variable assignments.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Lenses provides a functional approach to reading and modifying nested data structures without mutating the original data, using composable lens operations inspired by Haskell.
Yes, if you work regularly with nested data structures and want cleaner, more composable transformation code. The copyleft GPLv3+ license is a hard blocker for proprietary projects. The dormant maintenance status is a minor concern—the library is stable and has no known vulnerabilities, but you should verify that its behavior matches your needs before adopting it for new projects.
Install
lenses on PyPI
pip
pip install lensesuv
uv add lensespoetry
poetry add lensesInstalling lenses
Before you install
Installation is straightforward with no runtime dependencies. The package is in production/stable status but dormant—last release was over a year ago, though the repository remains active and supported on Python 3.7 through 3.11.
License in practice
Licensed under GPLv3+, a copyleft license. Any derivative work or distribution must also be open-source under compatible terms; this is a significant constraint for proprietary or closed-source projects.
Quickstart
pip install lenses
from lenses import lens
data = [{'name': 'Jane', 'scores': ['a', 'b']}, {'name': 'Zoe', 'scores': ['f', 'f']}]
format_scores = lens.Each()['scores'].Each().Instance(str).call_upper()
result = format_scores(data)
Requires Python 3.7 or later (supports up to 3.11).
Verify before relying
- Whether the dormant maintenance status affects real-world usability or indicates unaddressed issues.
- Performance characteristics on very large or deeply nested data structures compared to imperative approaches.
Package facts
| License | GPLv3+ (copyleft) |
| Python support | supports the current Python release (>=3.7, <4) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | dormant — 1,025 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 163,389/month — #10,571 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: lenses-1.2.0-py3-none-any.whl
Keywords: lens, lenses, immutable, functional, optics
Tags
More Libraries packages
urllib3 is an HTTP client library that provides…
permissive · top 100 on PyPI
requestsRequests is a Python HTTP library that…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
python-dateutilProvides parsing, arithmetic, and recurrence…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
oslashOSlash implements functional programming…
permissive · top 15,000 on PyPI
immutablesProvides an immutable mapping type backed by a…
permissive · top 5,000 on PyPI
returnsProvides functional programming…
permissive · top 5,000 on PyPI
itypesProvides immutable container types (Dict and…
permissive · top 5,000 on PyPI
purifyA decorator that makes object-mutating…
permissive · top 15,000 on PyPI
ExpressionExpression provides functional programming…
permissive · top 5,000 on PyPI
deepdiff6DeepDiff recursively compares two Python…
permissive · top 15,000 on PyPI
funcyFuncy provides a collection of functional…
permissive · top 5,000 on PyPI
sae-lensSAE Lens trains and analyzes sparse…
permissive · top 15,000 on PyPI
pyrsistentPyrsistent provides immutable, persistent data…
permissive · top 1,000 on PyPI