skillfed

lenses

A lens library for python

lenses v1.2.0 163.4K downloads/30d#10,571 on PyPI339
Copyleft license GPLv3+ DORMANT released

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 lenses

uv

uv add lenses

poetry

poetry add lenses

Installing 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

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)Natural Language :: EnglishProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPyTopic :: Software Development :: Libraries

Tags

immutable data structure manipulationfunctional lens librarynested data access without mutationcomposable optics for pythonfunctional programming data transformation
functional-programmingimmutable-datadata-transformation

More Libraries packages