PyComp — Python Comprehension Converter
Automatically converts traditional Python for loops into list, set, and dictionary comprehensions — with a preview diff before applying any change.
Powered by a local FastAPI + PostgreSQL backend that acts as an intelligent cache: identical code is transformed once and served instantly from cache on subsequent requests.
Features
- Convert loops to comprehensions — detects convertible patterns and rewrites them in place
- Preview before applying — shows a native VS Code diff view so you can review the change before committing
- List, set, and dict comprehensions — handles all three types, with and without
if conditions
- Smart cache — tracks every transformation in PostgreSQL; repeated code is served from cache instantly
- Transformation history — browse, insert, and clear past transformations from the Command Palette
- Context menu integration — right-click any selection in a Python file to convert it
- Keyboard shortcut —
Ctrl+Shift+L / Cmd+Shift+L
Requirements
PyComp requires a local backend to be running before use.
1. Install Python dependencies
uv sync
2. Configure your .env
DATABASE_URL=postgresql://user:password@localhost:5432/pylens
3. Run database migrations
uv run alembic upgrade head
4. Start the backend
uv run uvicorn pylens.app.main:app --port 8000
The extension connects to http://localhost:8000 by default.
Usage
Convert a loop
- Open a
.py file
- Select the code you want to convert (include the
name = [] initialization line)
- Press
Ctrl+Shift+L (Cmd+Shift+L on Mac) — or right-click → PyComp: Convert to List Comprehension
- Review the diff that appears on the right
- Click Apply to replace the code, or Cancel to discard
Patterns supported
List comprehension
# Before
result = []
for x in items:
result.append(x * 2)
# After
result = [x * 2 for x in items]
List comprehension with condition
# Before
evens = []
for n in range(20):
if n % 2 == 0:
evens.append(n)
# After
evens = [n for n in range(20) if n % 2 == 0]
Set comprehension
# Before
unique = set()
for word in words:
unique.add(word.lower())
# After
unique = {word.lower() for word in words}
Dictionary comprehension
# Before
squared = {}
for n in numbers:
squared[n] = n ** 2
# After
squared = {n: n ** 2 for n in numbers}
Patterns NOT converted (intentionally)
PyComp is conservative. It will not convert:
for/else loops — else clauses have no equivalent in comprehensions
- Loops with
break, continue, or return — semantics would change
- Loops with multiple statements in the body
- Loops where the accumulator variable is used inside the expression
- Pre-filled collections (
result = [1, 2], d = {"a": 1})
Commands
| Command |
Description |
PyComp: Convert to List Comprehension |
Convert selected code |
PyComp: Show Transformation History |
Browse past transformations |
PyComp: Clear Transformation History |
Delete all history from the database |
Extension Settings
| Setting |
Default |
Description |
pylens.apiUrl |
http://localhost:8000 |
URL of the PyComp FastAPI backend |
How the cache works
Every transformation is stored in PostgreSQL with a SHA-256 hash of the original code. When you submit the same code again, PyComp returns the cached result instantly and increments a hits counter — no AST parsing needed. You can inspect all cached transformations via PyComp: Show Transformation History.
License
MIT